Getting a parent element for a jquery selector

一个人想着一个人 提交于 2019-11-28 10:38:43

Use closest():

$('.anotherclass').click(function () {
     alert($(this).closest('tr').prop('id'));
});

Use closest:

$(".anotherclass").click(function() {
    console.log($(this).closest("tr").attr("id"));
});

From the docs:

Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.

you can use this.

$('.anotherclass').click(function () {
     alert($(this).parents('tr').attr("id"));
});
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!