Javascript 如何使用javascript查找父元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2454308/
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-23 00:26:15 来源:igfitidea点击:
How to find the parent element using javascript
提问by Achaius
I want to change the background color of the table cell when radio button inside the cell is clicked.
当单击单元格内的单选按钮时,我想更改表格单元格的背景颜色。
<table>
<tr>
<td align="center">
<input type="radio" value="foo"
onclick="this.parentElement.style.background-color='red';" />
</td>
</tr>
</table>
How to get the parent element reference?
如何获取父元素引用?
回答by Yuval Adam
Using plain javascript:
使用普通的javascript:
element.parentNode
In jQuery:
在 jQuery 中:
element.parent()
回答by ryanulit
Use the change event of the select:
使用select的change事件:
$('#my_select').change(function()
{
$(this).parents('td').css('background', '#000000');
});

