【问题标题】:Get some parent element ID in order to send to javascript function获取一些父元素 ID 以发送到 javascript 函数
【发布时间】:2012-01-31 07:38:35
【问题描述】:

我有这张桌子

<table id="tblId">
        <thead>
            <tr>
                <th>View Name</th>
                <th>View Description</th>                       
            </tr>
        </thead>
            <tbody>
                <tr id="1">
                    <td>Name</td>                    
                    <td><span onclick="jsFunction()">description</span></td>
                </tr>
                <tr id="2">
                    <td>Name</td>                    
                    <td><span onclick="jsFunction()">description</span></td>
                </tr>
            </tbody>
</table>

在每个跨度的 onclick 事件中,我需要向 js 函数发送该特定行的行 ID,我该怎么做?

谢谢!

【问题讨论】:

    标签: javascript dom javascript-events


    【解决方案1】:

    一种方法是您可以使用 jsFunction 发送“发送者”数据,如下所示,

    ... onclick="jsFunction(this)" ....
    

    在你的 jsFunction 中你可以找到 tr 元素,

        function jsFunction(sender){
            var tr = sender.parentNode.parentNode;
            alert( tr.getAttribute('id') );
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-18
      • 1970-01-01
      • 2020-01-27
      • 2020-07-13
      • 2012-05-03
      • 2012-04-12
      • 1970-01-01
      • 2017-08-24
      相关资源
      最近更新 更多