本文翻譯自: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="#" target="_blank" rel="external nofollow" 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...
如果你想重載原始的jQuerytoggle()
,我不建議... !(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 。