正如 eed3si9n 所说,这是.hide()
通话在通话之前.appendTo()
。
我想我会添加这个答案,因为我发现了另一种也可以使用的方法:
jQuery('<div></div>')
.css("display", "none")
.appendTo(document.body)
;
我不确切知道它是否像这样工作,但我想将元素添加到隐藏的 DOM 应该更快,因为浏览器不需要渲染它?
对于那些像我一样并且必须确切知道事情是如何运作的人的更新:
jQuery.hide() 的代码 - 我添加的评论
hide: function(speed,callback){
return speed ?
// this block of code won't be run, since speed will be undefined
this.animate({
height: "hide", width: "hide", opacity: "hide"
}, speed, callback) :
// this is the relevant code
this.filter(":visible").each(function(){
this.oldblock = this.oldblock || jQuery.css(this,"display");
// you can see here that it merely sets the display to 'none'
this.style.display = "none";
}).end();
}
这是选择器的代码:hidden
:
hidden: function(a) {
return "hidden" == a.type // <input type="hidden" />
|| jQuery.css(a, "display") == "none" // style.display = none
|| jQuery.css(a, "visibility") == "hidden"; // style.visibility = hidden
}
This doesn't really explain why Chrome isn't showing the element as hidden though...