我想获得一个Element的父级,它具有指定的标签名称.
示例代码:
<table>
<tr>
<td>
<input type='button' id='myId' />
</td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
现在我想要这样的东西:
$('#myId').specificParent('table'); //returns NEAREST parent of myId Element which table is it's tagname.
Run Code Online (Sandbox Code Playgroud)
jen*_*ram 103
获取与选择器匹配的第一个祖先元素,从当前元素开始并逐步向上遍历DOM树.
也就是说,
$('#myId').closest('table')
Run Code Online (Sandbox Code Playgroud)
(演示)