jQueryに相当する.hide()を使用して可視性を設定するhide()を使用して可視性を設定するhide()を使用します:非表示

5770 ワード

Equivalent of jQuery.hide() to set visibility: hidden
In jQuery, there are .hide() and .show() methods which sets the CSS display: none setting. jQueryでは、CSS .show()を設定する方法が.hide().show()である.
Is there an equivalent function which would set the display: none setting? visibility: hiddenを設定できる等価関数はありますか?
I know I can use visibility: hidden but I prefer some function like .css() or so. 私は.hide()を使うことができることを知っていますが、.css()のような関数が好きです.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 to use jQuery's toggleClass()methodより簡単な方法はjQueryのtoggleClass()メソッドを使用することです
CSS CSS
.newClass{visibility: hidden}

HTML HTML
Trigger Element
Some Content

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 .hide() 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. もちろん、ここでは純粋なjsを使用しているので、jQuery'each'(@JamesAllardice回答で与えられた)は実装されていません.
Working example is here . 仕事の例はここにあります.

#5階


Here's one implementation, what works like return el or $.prop(name[,value]) function. これは、$.attr(name[,value])または$.prop(name[,value])関数に類似した役割を果たす実装である.If $.attr(name[,value]) variable is filled, visibility is set according to that, and b is returned (allowing to continue with other properties), otherwise it returns visibility value. this変数が満たされている場合、可視性は、称に従ってbが(他の属性との継続を許可する)戻り、そうでない場合、可視性値が返される.
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 this , 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 .