如何檢查具有特定數據屬性的父元素是否是「最後一個子元素」本身?基本上,要記住,他們會產生,大多數<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>
非常感謝