我有一张表,我在其中动态添加一些行,并在新行中添加一个输入文本元素。当输入元素中的文本发生变化时,我希望访问父行并对其执行操作。但是我的代码无法访问输入元素的父节点。以下是代码示例。这是什么原因?有一些解决办法吗?为什么输入元素的父节点未定义?
行的创建看起来是这样的:
var newRow = document.createElement('tr');
dojo.place(newRow, row, "after");
var td = document.createElement('td');
dojo.place(td, newRow, 'last');
dojo.attr(td, 'colspan', 2);
dojo.place(document.createTextNode('Track id:'), td, 'last');
var input = document.createElement('input');
dojo.place(input, td, 'last');
dojo.attr(input, 'type', 'text');
dojo.attr(input, 'value', 0);
dojo.attr(input, 'onchange', "JavaScript: onTrackIdChange(event);");
的onTrackIdChange
的初始部分看起来像:
function onTrackIdChange(event)
{
var textbox = event.target;
console.log(textbox);
// lets find the parent row
var row = findParentRow(btn);
findParentRow的实现看起来像:
function findParentRow(node)
{
var parent = node;
do
{
console.log(parent);
parent = parent.parentNode;
}
while (parent.nodeName != 'TR');
return parent;
}
的findParentRow函数失败,因为它说输入元素的父项nt没有定义。
哦,那是一个愚蠢的错误。复制粘贴操作的结果:(非常感谢。 – 2009-12-14 07:04:54