admin管理员组文章数量:1377554
I am trying to access the nearest div from the anchor tag that has a data-id attribute.
.parent().parent().parent()
works but I am really trying to avoid doing that.
<div class="panel panel-default" data-id="@customer.Id">
<div class="panel-heading clearfix">
@customer.Name
<div class="pull-right">
<a class="btn btn-sm btn-default js-deleteCustomer" href="#" title="Delete">
<i class="fa fa-trash" aria-hidden="true"></i>
</a>
</div>
</div>
I tried the following:
var customerId = $(this).closest("div:has(*[data-id])").attr('data-id');
I am trying to access the nearest div from the anchor tag that has a data-id attribute.
.parent().parent().parent()
works but I am really trying to avoid doing that.
<div class="panel panel-default" data-id="@customer.Id">
<div class="panel-heading clearfix">
@customer.Name
<div class="pull-right">
<a class="btn btn-sm btn-default js-deleteCustomer" href="#" title="Delete">
<i class="fa fa-trash" aria-hidden="true"></i>
</a>
</div>
</div>
I tried the following:
var customerId = $(this).closest("div:has(*[data-id])").attr('data-id');
Share
Improve this question
edited Jul 9, 2017 at 16:55
BoltClock
725k165 gold badges1.4k silver badges1.4k bronze badges
asked Jul 9, 2017 at 16:50
Blake RivellBlake Rivell
13.9k33 gold badges130 silver badges261 bronze badges
1 Answer
Reset to default 7You want
var customerId = $(this).closest("div[data-id]").attr('data-id');
:has(*[data-id])
doesn't match an element with that attribute. It matches an element that contains another element with that attribute. The element that's matched probably doesn't have this same attribute, which would explain why :has(*[data-id])
doesn't work. For instance, it would match the outer div, not the inner div, in the following:
<div>
<div data-id="@customer.Id"></div>
</div>
To match an element that has an attribute, simply attach the attribute selector directly without using :has()
.
版权声明:本文标题:javascript - Finding the data attribute value of a parent div several levels up with jQuery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744443432a2606544.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论