Javascript:未捕获的类型错误:无法设置未定义的属性‘className’
2011-08-18
8050
我有一个带有 onclick 函数的表。单击时出现以下错误:
未捕获 TypeError:无法设置未定义的属性“className”
奇怪的是,5 行中有 3 行
class
已更改,但 2/5 行未更改。再次单击该行时,其余两行中的一行会更改,再次单击时,最后一行会更改。这是我的代码:
HTML 表格
<tr class='invoice-tr-standard row5' onClick="FormTRClick(event, '6', '5')"><td><input type='checkbox' name='strFormFactuur[]' value='19500' id='chk6'> <img src='../images/link.png' border='0'></td>
<td><img src='../images/b_save.png' border='0' height='13px' style='cursor: pointer;' onClick="saveData('6', '?iID=19500&bType=0', 'all');")> <input type='image' src='../images/toevoegen.png' border='0' style='float: right; cursor: pointer;' name='FormDupliceer' value='19500'></td>
</tr>
<tr class='invoice-tr-standard row5' onClick="FormTRClick(event, '7', '5')"><td></td>
<td><img src='../images/b_save.png' border='0' height='13px' style='cursor: pointer;' onClick="saveData('7', '?iID=36&bType=1', 'fltBedrag');"> <input type='image' src='../images/b_delete.png' border='0' style='float: right; cursor: pointer;' name='FormVerwijder' value='36'></td>
</tr>
<tr class='invoice-tr-standard row5' onClick="FormTRClick(event, '8', '5')"><td></td>
<td><img src='../images/b_save.png' border='0' height='13px' style='cursor: pointer;' onClick="saveData('8', '?iID=37&bType=1', 'fltBedrag');"> <input type='image' src='../images/b_delete.png' border='0' style='float: right; cursor: pointer;' name='FormVerwijder' value='37'></td>
</tr>
<tr class='invoice-tr-standard row5' onClick="FormTRClick(event, '9', '5')"><td></td>
<td><img src='../images/b_save.png' border='0' height='13px' style='cursor: pointer;' onClick="saveData('9', '?iID=38&bType=1', 'fltBedrag');"> <input type='image' src='../images/b_delete.png' border='0' style='float: right; cursor: pointer;' name='FormVerwijder' value='38'></td>
</tr>
<tr class='invoice-tr-standard row5' onClick="FormTRClick(event, '10', '5')"><td></td>
<td><img src='../images/b_save.png' border='0' height='13px' style='cursor: pointer;' onClick="saveData('10', '?iID=66&bType=1', 'fltBedrag');"> <input type='image' src='../images/b_delete.png' border='0' style='float: right; cursor: pointer;' name='FormVerwijder' value='66'></td>
</tr>
javascript 函数
FormTRClick()
:
function FormTRClick(event, uID, PCID) {
var Object= event.target.tagName;
if (Object == 'TD') {
var Elements = document.getElementsByClassName('row' + PCID);
var iNumElements = Elements.length;
alert(iNumElements); // Outputs 5 as expected
for (var i=0; i < iNumElements; i++) {
Elements[i].className = "invoice-tr-clicked"; // Only changes 3 out of 5 on the first click
}
}
}
更新
当使用以下
Elements[i].className += " invoice-tr-clicked";
而不是
Elements[i].className = "invoice-tr-clicked";
时,脚本可以正常工作。但是当替换(因此为
=
)时,我收到
无法设置未定义的属性“className”
错误。
1个回答
我已通过将
Elements[i].className = "invoice-tr-clicked";
替换为
Elements[i].className = "invoice-tr-clicked row" + PCID;
来修复此问题。>
现在它已经可以正常工作,这没有任何意义,因为 demo 可以正常工作。
Devator
2011-08-18