Jus*_*tin 4 html javascript jquery hide
我想在.social-menu中隐藏任何具有空白href的LI.
我最初有:
$('ul.social-menu li a[href*=""]').hide();
Run Code Online (Sandbox Code Playgroud)
但它只隐藏了链接.
我想也许我可以使用:
$('ul.social-menu li a[href*=""]').addClass('hidden')
Run Code Online (Sandbox Code Playgroud)
但它并没有隐藏类.
HTML是:
<ul class="social-menu">
<li class="facebook"><a target="parent" href=""></a></li>
<li class="twitter"><a target="parent" href="http://twitter.com/#!/dft_au">Twitter</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
使用的:has()选择器或所述has()方法来选择所有<li>包含一个锚(元件<a>)具有空href属性值:
$('ul.social-menu li:has(a[href=""])').hide();
// or…
$('ul.social-menu li').has('a[href=""]').hide();
Run Code Online (Sandbox Code Playgroud)
请注意,.has()它比:has()以下更有效:http://jsperf.com/jquery-has-vs-has虽然:has()稍微更具可读性恕我直言.