如何检查具有特定数据属性的父元素是否是“最后一个子元素”本身?基本上,要记住,他们会产生,大多数<tbody>
的数据属性 - data-state="closed"
- 我想检查他们中的一个是否是最后一个,当我点击一个子元素,这是一个链接,这是在里面。如何检查元素的父元素本身是否是使用javascript或jquery的最后一个孩子
JS/jQuery的:
$('body').delegate('a[data-view="showhide"]', 'click', function (e) {
var thisElem = $(this),
thisTbody = thisElem.closest('tbody');
e.preventDefault();
//check to see if this element's parent is a last child
if (thisTbody.filter('[data-state]').is(':last')) {
console.log('this is the last tbody of its kind called \'data-state\'');
//...Do something
}
});
HTML:
<table>
<tbody data-state="closed">
<tr><td><a href="#" data-view="showhide">cell 1</a></td></tr>
</tbody>
<tbody data-state="closed">
<tr><td><a href="#" data-view="showhide">cell 2</a></td></tr>
</tbody>
<tbody data-state="closed">
<tr><td><a href="#" data-view="showhide">cell 3</a></td></tr>
</tbody>
<tbody>
<tr><td>cell not important</td></tr>
</tbody>
</table>
非常感谢