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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8692374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:05:57  来源:igfitidea点击:

Get some parent element ID in order to send to javascript function

javascriptdomjavascript-events

提问by Ovi

I have this table

我有这张桌子

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

In the onclick event of each span i need to send the js function the row id of that specific row, How do i do that?

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

Thank you!

谢谢!

回答by tcak

One way is you can send "sender" data with jsFunction as below,

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

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

In your jsFunction you can find tr element,

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

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