:not() 以各种形式不起作用
我正在制作一个简单的 jQuery 导航系统,但我远非这方面的专家,因为下面的代码可能会演示..
HTML:
<ul id="main-nav">
<li><a href="../" id="home">HOME</a></li>
<li class="pipe">|</li>
<li id="about">ABOUT US</li>
<li class="pipe">|</li>
<li id="projects">PROJECT TYPES</li>
<li class="pipe">|</li>
<li id="reducing">REDUCING EMISSIONS</li>
<li class="pipe">|</li>
<li id="carbon">CARBON MARKETS</li>
<li class="pipe">|</li>
<li id="FAQs">FAQs</li>
</ul>
jQuery:
$(function () {
$("#main-nav li:not('.pipe')").hover(function () {
var $this = $(this).attr("id");
$('#nav-strip2 ul.sub-nav').hide();
$("#nav-" + $this).show();
});
});
显示/隐藏效果很好,唯一的问题是当管道悬停在它上方时会隐藏一切。菜单需要用
来组成,而不能只是
,这是有原因的,但它并不是真正相关且很长。我试图在这里排除 .hover() 的事情发生,当它是一个带有 .pipe 类的 li 时,但没有什么乐趣。我做错了什么?任何帮助表示赞赏。我确信有一种方法可以排除没有附加 ID 的
,这样可以节省将 .pipe 类分配给所有这些
谢谢。
I'm making a simple jQuery navigation system but I am far from expert at it, as the following code may demonsytrate..
HTML:
<ul id="main-nav">
<li><a href="../" id="home">HOME</a></li>
<li class="pipe">|</li>
<li id="about">ABOUT US</li>
<li class="pipe">|</li>
<li id="projects">PROJECT TYPES</li>
<li class="pipe">|</li>
<li id="reducing">REDUCING EMISSIONS</li>
<li class="pipe">|</li>
<li id="carbon">CARBON MARKETS</li>
<li class="pipe">|</li>
<li id="FAQs">FAQs</li>
</ul>
jQuery:
$(function () {
$("#main-nav li:not('.pipe')").hover(function () {
var $this = $(this).attr("id");
$('#nav-strip2 ul.sub-nav').hide();
$("#nav-" + $this).show();
});
});
The showing/hiding works just fine, the only issue is when the pipe is hovered over it hides everything. There's a reason the menu needs to be made up in <li>
s and can't just be <a>
s but it's not really relevant and long.
I'm trying here to exclude the .hover() stuff from happening when it's a li with .pipe class but there's no joy. What am I doing wrong? Any help appreciated. I'm sure there's a way of excluding <li>
s with no ID attached, which would save assigning the .pipe class to all those <li>
s. Alas I have not the jQuery ability to figure this out yet!
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当使用
:not()
作为选择器时,不要使用括号内的引号:When using
:not()
as a selector, don't use quotes within its brackets:试试这个:
try this: