2011-01-06 162 views
0

我从使用通用的JavaScript设置一个CSS样式属性jQuery的hide()和.css('display','none')之间的区别?

element.style.display = "none"; 

到jQuery的方法

element.hide(); 

当按下后退按钮用于返回到我的网页改变了几页,元素被隐藏使用jQuery不再隐藏。当我使用原始JavaScript设置显示时,当我返回页面时,隐藏的元素将保持隐藏状态。

是否有解决方法,让jQuery的hide()函数以同样的方式工作?

回答

9

jQuery的hide方法执行

 for (i = 0; i < j; i++) { 
      this[i].style.display = "none"; 
     } 

你有一些其他的问题。

完整的方法是

hide: function(speed, easing, callback) { 
    if (speed || speed === 0) { 
     return this.animate(genFx("hide", 3), speed, easing, callback); 

    } else { 
     for (var i = 0, j = this.length; i < j; i++) { 
      var display = jQuery.css(this[i], "display"); 

      if (display !== "none") { 
       jQuery.data(this[i], "olddisplay", display); 
      } 
     } 

     // Set the display of the elements in a second loop 
     // to avoid the constant reflow 
     for (i = 0; i < j; i++) { 
      this[i].style.display = "none"; 
     } 

     return this; 
    } 
}, 
+0

绝对正确......有另一种方法嵌套在那里'缓存'显示属性。谢谢! – 2011-01-06 20:23:14

0

这是一样的。 jQuery隐藏应用display:none;到元素。什么触发你的皮?它在document.ready中吗?这似乎是当你点击'后退'它不执行JavaScript。

3

有一个巨大的差别:
.hide()称为显示属性的值保存在jQuery的数据缓存,所以当.show()被调用时,初始显示值恢复!