我想获得一个具有指定标签名称的元素的父级。
示例代码:
<table> <tr> <td> <input type='button' id='myId' /> </td> </tr> </table>
现在我想要这样的东西:
$('#myId').specificParent('table'); //returns NEAREST parent of myId Element which table is it's tagname.
解决方法
见
.closest()
:
Get the first ancestor element that matches the selector,beginning at the current element and progressing up through the DOM tree.
也就是说,
$('#myId').closest('table')
(Demo)