javascript - 使用 jQuery 为表格行设置动画背景颜色

标签 javascript jquery jquery-ui html-table jquery-animate

这是我的 code .

Javascript:

var table = document.getElementById("Table-1");
var rowCount = table.rows.length;



for(var i=0;i<6;i++) {


row = table.insertRow(rowCount);
cell1 = row.insertCell(0);
cell1.name = "animate";
cell1.id = i ;
var content = document.createElement("output");                
content.innerHTML = i ;
cell1.appendChild(content);
rowCount++;

  // if (i%2 == 0) {
       setInterval(function() {
           $(input[name="animate"]).animate( { backgroundColor: '#f08080' }, 'slow')
           .animate( { backgroundColor: 'red' }, 'slow'); 
                 }, 1000);
   // }

}​

HTML:

<table id="Table-1" border="1">

                    <tr>
                        <th><center>List</center></th>
                    </tr>
</table> 

我用 javascript 构建了我的表,我想每秒为几行设置动画,但它不适用于所有行。但是,当我为特定行设置动画时,它会起作用。

谢谢。

最佳答案

你的脚本有几个问题:

  • 您创建 output 元素而不是 input
  • 您将 td 命名为 td,但稍后您在选择器中引用了 input
  • 您在选择器中缺少撇号
  • 你无缘无故地在循环中启动了多个动画
  • 您将 vanilla javascript 与 jquery 混合使用(这只是装饰品)

将选择器更改为:

setInterval(function() {
    $('table td input').animate({
        backgroundColor: '#f08080'
    }, 'slow').animate({
        backgroundColor: 'red'
    }, 'slow');
}, 1000);

查看更新 FIDDLE .

关于javascript - 使用 jQuery 为表格行设置动画背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12920087/

相关文章:

javascript - 如何捕获 HTML 对象上的点击事件?

javascript - 网页不会自动调整到任何屏幕高度

jquery - 修复了可调整大小的 jquery-ui 对话框中的页眉和页脚?

javascript - 标签文本框的制作方法

javascript - 如何将 ACE 与我的 ASP.NET MVC 应用程序一起使用?

Javascript - Sweetalert - 添加值到选择列表

javascript - JS 和正则表达式 : how to replace punctuation pattern properly?

javascript - JavaScript 中的 AJAX 跨域 ASMX Web 服务 JSONP 调用出错

javascript - 如何在 JavaScript 中解析 JSON 数组

javascript - 重新单击输入字段时 jQuery Datepicker 挂起(Chrome)