JavaScript如何解释索引?

时间:2010-09-15 19:04:43

标签: javascript jquery html dom

function highlightRow(searchRow) 
{                       
      if(searchRow != null)
      {
         var oRows = document.getElementById('Table').getElementsByTagName('tr');
         //use row number to highlight the correct HTML row.
         var lastTwo = searchRow;
         lastTwo = lastTwo.slice(-2); //extract last two digits         

         oRows[lastTwo].style.background="#90EE90";
       }       
}

我分页了100行。

当searchRow返回55时,我突出显示第55行 当searchRow返回177时,我突出显示第77行,因此切片功能。

现在问题:
当searchRow返回03或201时,oRows []上的索引不起作用。

这是为什么?

当我硬编码“03”时,为了让我更加困惑,它确实有效:

oRows[03].style.background="#90EE90";  

有关其如何运作的任何见解?

jQuery是否有这个问题?

谢谢。

1 个答案:

答案 0 :(得分:2)

你正在处理一个字符串,你需要一个整数,如下所示:

oRows[parseInt(lastTwo, 10)].style.background = "#90EE90";

You can give it a try here

要做一个准确的测试以查看它的实际效果,您会看到它的工作原理:

oRows[03].style.background="#90EE90";

但是这个(现在实际发生的事情)不会:

oRows["03"].style.background="#90EE90";
相关问题