Heyho,
我想解决不同段落的父元素。由于段落以不同的方式嵌套,我无法使用"最接近"或 sth 以相同的方式解决。
这就是为什么我尝试使用正则表达式测试来解决#text-test
和#img-test
:
<div class="text-test">
<p class"tester">texttest</p>
</div>
<div class="img-test">
<div class="wrapper">
<p class="tester">imgtest</p>
</div>
</div>
$('.tester').click(function() {
//$(this).closest('div').addClass('active');
/* get the closest div where the class ends up with -test
* and add class active
* wrapperdiv = $(this).closest('div').attr('class').test(/-test$/);
*/
//console.log(wrapperdiv)
});
您有什么建议吗,我如何使用测试方法解决?
干杯
$(this).closest('div[class$="-test"]').addClass('active');
[name$=value]
称为"带选择器结束"。对于上述情况,它将选择那些div
,其类以 -test
结尾。