。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。
jquery的hide方法相当于display:none,show方法相当于display:'';
jquery的源码实现hide,show方法代码
function showHide( elements, show ) {
var display, elem, hidden,
values = [],
index = 0,
length = elements.length;
for ( ; index < length; index++ ) {
elem = elements[ index ];
if ( !elem.style ) {
continue;
}
values[ index ] = jQuery._data( elem, "olddisplay" );
display = elem.style.display;
if ( show ) {
// Reset the inline display of this element to learn if it is
// being hidden by cascaded rules or not
if ( !values[ index ] && display === "none" ) {
elem.style.display = "";
}
// Set elements which have been overridden with display: none
// in a stylesheet to whatever the default browser style is
// for such an element
if ( elem.style.display === "" && isHidden( elem ) ) {
values[ index ] = jQuery._data( elem, "olddisplay", defaultDisplay(elem.nodeName) );
}
} else {
hidden = isHidden( elem );
if ( display && display !== "none" || !hidden ) {
jQuery._data( elem, "olddisplay", hidden ? display : jQuery.css( elem, "display" ) );
}
}
}
// Set the display of most of the elements in a second loop
// to avoid the constant reflow
for ( index = 0; index < length; index++ ) {
elem = elements[ index ];
if ( !elem.style ) {
continue;
}
if ( !show || elem.style.display === "none" || elem.style.display === "" ) {
elem.style.display = show ? values[ index ] || "" : "none";
}
}
return elements;
}
show: function() {
return showHide( this, true );
}
hide: function() {
return showHide( this );
}
注意:在showHide()传入的第二形参为show===true时,进入的
第一个判断设置如果内联样式display === ‘none’就设置display:‘’;
第二个判断如果不是内联样式,会根据元素本身是inline,inline-block,block来设置不同的值,
比如是div你在css中设置的是display:none,现在采用show方法就会给元素设置行内样式display:block,
如果是a等行内元素,show方法设置的就是display:inline;
表现效果差不多。但是show方法执行,就必须要display:none存在,不管是内联样式,嵌入样式,外部样式。否则show方法不会对元素进行设置。
我觉得hide()和show()更灵活些。
是的呀,老铁,效果一样
效果是一样的;$("#id").hide()实际上是设置了css中的display为none
本质不一样!看起来效果是一样! jq show 与hide 相当于 显示跟隐藏
但是 css中 block:块级元素 none:不生成!
你在display中可以设置 inline inline-block block none 前三个都是可以显示的 行内元素 行内块级元素 块级元素 none:不生成
这么说吧,hide()和show()是动态的,比较灵活,比方说你要让一张图片动态显示隐藏,就可以用到它的回调函数,
比如:
$("img").show(3000,function(){
$(this).css("border","solid 1px #ccc");
})
})
$("img").click(function(){
$(this).hide(3000);
})
当然我这里只是简单给了一个例子,你可以在回调function中做很多事情的。