Javascript 使用jquery更改计时器事件上的表行背景色
我基本上有来自JSP的标记。我在每一行中添加类,我希望每一行都有闪烁效果Javascript 使用jquery更改计时器事件上的表行背景色,javascript,jquery,Javascript,Jquery,我基本上有来自JSP的标记。我在每一行中添加类,我希望每一行都有闪烁效果 <table> <tbody> <tr class="blinkYellow"> <td>Col 1</td> <td>Col 2</td> <td>Col 3</td> </tr> <tr> <td>Col 1</td> &
<table>
<tbody>
<tr class="blinkYellow">
<td>Col 1</td>
<td>Col 2</td>
<td>Col 3</td>
</tr>
<tr>
<td>Col 1</td>
<td>Col 2</td>
<td>Col 3</td>
</tr>
<tr class="blinkYellow">
<td>Col 1</td>
<td>Col 2</td>
<td>Col 3</td>
</tr>
</tbody>
</table>
我查看了Firebug HTML选项卡,注意到所选元素行上的背景颜色确实发生了变化
但我不确定为什么行的背景色没有从黄色和白色切换颜色 attr函数将添加/更改属性。使用
css
函数代替attr
函数
$(document).ready(function(){
setInterval(findYellow,1000);
function findYellow(){
$("tr.blinkYellow").each(function(){
var $this = $(this);
if($this.css("background-color") == "yellow"){
$this.css("background-color", "white")
}else{
$this.css("background-color", "yellow")
}
})
}
});
另一个选项是为blinkYellow
类设置css样式,该类包含黄色的背景色,然后切换:
var $blinkYellow = $("tr.blinkYellow");
$(document).ready(function(){
setInterval(findYellow, 1000);
function findYellow() {
$blinkYellow.each(function() {
// note: jQuery is not really needed here - instead, you could use the following line
// this.className = this.className == 'blinkYellow' ? '' : 'blinkYellow';
$(this).toggleClass('blinkYellow');
});
}
});
使用css类添加颜色不是更好吗。如果这样做,可以按如下方式使用jquery:
$(this).toggleClass("blink-yellow");
编辑:
您可以使用此页面尝试这些事情
在小提琴中,它看起来会很好地工作;-) 同意,这将是一个更好的方法。。。非常感谢,一切正常!JSFIDLE链接太棒了..已经为它添加了书签。以前我使用notepad++创建示例html标记,但使用此工具,现在一切都很简单。。。谢谢你帮助像我们这样的新手。。请继续!是不是背景色而不是背景色???@webfac同样,jQuery可以同等地解释多个单词属性的CSS和DOM格式。例如,jQuery理解并返回.css('background-color')和.css('backgroundColor')的正确值。@Nate-感谢链接,我学到了一些新东西,哇。)@webfac和我一样。我过去只使用css样式表名称来实现该功能,我不知道DOM的名称也可以使用:)@Nate。。谢谢因为你的帖子,我了解了attr和css之间的区别。。。我也试着将此标记为一个答案,但我刚刚发现,在SO中,只有一个是允许的。不过我会把你的想法写在我未来的剧本里。。非常感谢。
$(this).toggleClass("blink-yellow");
$(document).ready(function(){
setInterval(findYellow,1000);
function findYellow(){
$("tr.blinkYellow").each(function(){
$(this).toggleClass("background-color-yellow");
})
}
});