【问题标题】:parentNode in Javascript doesn't seem to work for meJavascript 中的 parentNode 似乎对我不起作用
【发布时间】:2010-02-25 08:59:25
【问题描述】:

我在 gridview 中使用了一个文本框,它的 onkeyup 功能似乎不起作用....

这是我的网格视图

<asp:TemplateField>
  <HeaderStyle Width="12%" />
  <HeaderTemplate>
   Advance Detucted
   </HeaderTemplate>
   <ItemTemplate>
    <asp:TextBox ID="TxtAdvanceDeducted" runat="server"  
CssClass="text_box_height_14_width_50" onkeyup="check('this');"></asp:TextBox>
  </ItemTemplate>
   <ItemStyle Width="12%" HorizontalAlign="Center"  />
   </asp:TemplateField>

还有我的 javascript 函数,

var table = el.parentNode.parentNode.parentNode;
for (var y = 0; y < table.rows.length; y++) 
{
    for (var x = 0; x < table.rows[y].cells.length; x++) 
     {
        if (table.rows[y].cells[x] == el) 
        {
            alert("Row:" + y + " Cell: " + x);
        }
    }
}

当通过 webdeveloper 工具栏检查时,我得到了错误,

el.parentNode is undefined

任何建议...

alert(table.rows.length) 给了我 3... 但我有 2 行 + 1 个标题行...

【问题讨论】:

    标签: javascript gridview textbox onkeyup


    【解决方案1】:

    替换

    onkeyup="check('this');" // you are passing a string 'this' to the function.
    

    onkeyup="check(this);" // you are passing a reference of the element.
    

    【讨论】:

    • 在第一个和第二个 for 循环中添加一些警报以进行检查
    【解决方案2】:

    这是作为字符串而不是对象传递的,也许应该是:

    onkeyup="检查(这个)

    不过,这可能归结为 asp 语法。一个很好的测试方法是警告或在 firebug 中使用 console.log 来找出传递给函数的内容,例如

    console.log(el);
    

    alert(el);
    

    作为函数的第一行

    【讨论】:

      【解决方案3】:

      我不熟悉 ASP:this 周围的引号是必需的吗?

      check('this') => check(this)
      

      问候,
      斯蒂金

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多