获取一些父元素ID以便发送到javascript函数

时间:2012-01-01 08:20:21

标签: javascript dom javascript-events

我有这张桌子

<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>

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

谢谢!

1 个答案:

答案 0 :(得分:23)

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

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

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

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