覆盖CSS:使用jquery显示无属性

对于在css中定义为display:none的元素,我试图运行一个函数,该函数尝试使用.show()显示元素,代码如下:

CSS

.element { position: absolute; display: none; left: 0; top: 0; } 

HTML

 
Some content here
Some content here

jQuery的

 var c = $('.element'); c.each(function () { c.css({ 'left': dleft + 'px' }); c.css({ 'top': dtop + 'px' }); c.setTimeout(function () { c.show(1000); }, sduration); 

所有变量都已填充,我通过提醒他们所有变量并给出了默认值,但不知何故,元素在超时后没有显示。

您编写的代码中似乎存在两个问题。 你没有关闭.each()循环..另外setTimeout是一个全局函数.. ..检查这个

  var dleft = 40; var dtop = 40; var sduration = 1000; var c = $('.element'); c.each(function() { c.css({ 'left': dleft + 'px', 'top': dtop + 'px' }); }); setTimeout(function() { c.show(1000); }, sduration);​ 

检查这个FIDDLE

setTimeout是一个全局函数。

 var c = $('.element'); c.each(function () { c.css({ 'left': dleft + 'px', 'top': dtop + 'px' }); setTimeout(function () { c.show(1000); }, sduration); }); 

演示: http : //jsfiddle.net/S7nw3/

如果在显示元素时需要延迟,还可以使用jQuery .delay()函数。

 c.each(function() { c.css({ 'left': dleft + 'px', 'top': dtop + 'px' }); }); c.delay(1000).show();