[英]jQuery target only li tags where a element have a certain class
Is there a way to target only li elements where the a tag has a specific class? 有没有一种方法可以仅将标签具有特定类的li元素作为目标?
for example: 例如:
<li> <a href="#" class="mobile-only">link</a> </li>
<li> <a href="#" class="other-class">link</a> </li>
Can I just target the li where a has class "mobile-only"? 我可以只针对具有“仅限移动设备”课程的li吗?
you can access the parent of anchor tag with class mobile-only
您可以使用
mobile-only
类访问锚标记的父级
$( "a.mobile-only" ).parent();
or you can use parent pseudo selector , but there are performance concerns 或者您可以使用父级伪选择器 ,但是存在性能问题
li a:parent { background: none; }
js Code $( "li a:parent" )
js代码
$( "li a:parent" )
also the has selector 还有选择器
li:has(a.mobile-only) { background: none; }
js Code $( "li:has(a.mobile-only)" )
js代码
$( "li:has(a.mobile-only)" )
This is very easy to do with jQuery. 使用jQuery非常容易做到。
Just do: 做就是了:
$("a.mobile-only").parents("li")
$('a.mobile-only').parent("li")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.