相当于jQuery .hide()来设置可见性:隐藏

本文翻译自:Equivalent of jQuery .hide() to set visibility: hidden

In jQuery, there are .hide() and .show() methods which sets the CSS display: none setting. 在jQuery中,有.show() .hide().show()方法设置CSS display: none设置。

Is there an equivalent function which would set the visibility: hidden setting? 是否有一个等效函数可以设置visibility: hidden设置?

I know I can use .css() but I prefer some function like .hide() or so. 我知道我可以使用.css()但我更喜欢像.hide()这样的函数。 Thanks. 谢谢。


#1楼

参考:https://stackoom.com/question/eLCY/相当于jQuery-hide-来设置可见性-隐藏


#2楼

If you only need the standard functionality of hide only with visibility:hidden to keep the current layout you can use the callback function of hide to alter the css in the tag. 如果您只需要隐藏标准功能,只需使用visibility:hidden来保持当前布局,您可以使用hide的回调函数来更改标记中的css。 Hide docs in jquery 在jquery中隐藏文档

An example : 一个例子 :

$('#subs_selection_box').fadeOut('slow', function() {
      $(this).css({"visibility":"hidden"});
      $(this).css({"display":"block"});
});

This will use the normal cool animation to hide the div, but after the animation finish you set the visibility to hidden and display to block. 这将使用正常的酷动画来隐藏div,但在动画完成后,您将可见性设置为隐藏并显示为块。

An example : http://jsfiddle.net/bTkKG/1/ 一个例子: http//jsfiddle.net/bTkKG/1/

I know you didnt want the $("#aa").css() solution, but you did not specify if it was because using only the css() method you lose the animation. 我知道你不想要$(“#aa”)。css()解决方案,但你没有指定是否因为只使用css()方法而丢失了动画。


#3楼

An even simpler way to do this is to use jQuery's toggleClass() method 更简单的方法是使用jQuery的toggleClass()方法

CSS CSS

.newClass{visibility: hidden}

HTML HTML

<a href="#" class=trigger>Trigger Element </a>
<div class="hidden_element">Some Content</div>

JS JS

$(document).ready(function(){
    $(".trigger").click(function(){
        $(".hidden_element").toggleClass("newClass");
    });
});

#4楼

Pure JS equivalent for jQuery hide()/show() : jQuery hide()/ show()的纯JS等价物:

function hide(el) {
    el.style.visibility = 'hidden';    
    return el;
}

function show(el) {
    el.style.visibility = 'visible';    
    return el;
}

hide(document.querySelector(".test"));
// hide($('.test')[0])   // usage with jQuery

We use return el due to satisfy fluent interface "desing pattern". 我们使用return el来满足流畅的界面 “设计模式”。

Here is working example . 这是一个有效的例子


Below I also provide HIGHLY unrecommended alternative, which is however probably more "close to question" answer: 下面我也提供了高度未经推荐的替代方案,但可能更接近“质疑”答案:

HTMLElement.prototype.hide = function() {
    this.style.visibility = 'hidden';  
    return this;
}

HTMLElement.prototype.show = function() {
    this.style.visibility = 'visible';  
    return this;
}

document.querySelector(".test1").hide();
// $('.test1')[0].hide();   // usage with jQuery

of course this not implement jQuery 'each' (given in @JamesAllardice answer) because we use pure js here. 当然,这并没有实现jQuery'each '(在@JamesAllardice回答中给出),因为我们在这里使用纯js。

Working example is here . 工作范例就在这里


#5楼

Here's one implementation, what works like $.prop(name[,value]) or $.attr(name[,value]) function. 这是一个实现,它的作用类似于$.prop(name[,value])$.attr(name[,value])函数。 If b variable is filled, visibility is set according to that, and this is returned (allowing to continue with other properties), otherwise it returns visibility value. 如果b变量被填充,可视性根据称定, this返回(允许继续与其他属性),否则返回可见性值。

jQuery.fn.visible = function (b) {
    if(b === undefined)
        return this.css('visibility')=="visible";
    else {
        this.css('visibility', b? 'visible' : 'hidden');
        return this;
    }
}

Example: 例:

$("#result").visible(true).on('click',someFunction);
if($("#result").visible())
    do_something;

#6楼

You could make your own plugins. 你可以制作自己的插件。

jQuery.fn.visible = function() {
    return this.css('visibility', 'visible');
};

jQuery.fn.invisible = function() {
    return this.css('visibility', 'hidden');
};

jQuery.fn.visibilityToggle = function() {
    return this.css('visibility', function(i, visibility) {
        return (visibility == 'visible') ? 'hidden' : 'visible';
    });
};

If you want to overload the original jQuery toggle() , which I don't recommend... 如果你想重载原始的jQuery toggle() ,我不建议...

!(function($) {
    var toggle = $.fn.toggle;
    $.fn.toggle = function() {
        var args = $.makeArray(arguments),
            lastArg = args.pop();

        if (lastArg == 'visibility') {
            return this.visibilityToggle();
        }

        return toggle.apply(this, arguments);
    };
})(jQuery);

jsFiddle . jsFiddle

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值