2009-12-11 82 views
0

我有一张表,我在其中动态添加一些行,并在新行中添加一个输入文本元素。当输入元素中的文本发生变化时,我希望访问父行并对其执行操作。但是我的代码无法访问输入元素的父节点。以下是代码示例。这是什么原因?有一些解决办法吗?为什么输入元素的父节点未定义?

行的创建看起来是这样的:

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没有定义。

回答

3

在你的通话中,findParentRow(btn),你不应该通过textbox而不是btn

在你提交的代码的其他地方没有提到这个btn变量,所以如果这是有序的,你应该发布一些更多的代码。

+0

哦,那是一个愚蠢的错误。复制粘贴操作的结果:(非常感谢。 – 2009-12-14 07:04:54

0

父没有定义,因为节点没有定义。节点没有在findParentRow中定义,因为btn没有在onTrackIdChange中定义。