尝试使用 jQuery 的 .addClass 方法进行导航
我有一个 Nav,其中我尝试使用 jQuery 的 addClass 方法来设置上次单击的链接的链接颜色。问题是我必须在导航中的所有其他链接上使用removeClass。这就是我遇到的麻烦。
我以一种幼稚的方式编写了代码,但知道这不是好的编程。下面是带有样式表参考的代码。
jQuery('#shop-nav').click(function(){
jQuery("#shop-nav").addClass("clicked");
jQuery("#art-nav").removeClass("clicked");
jQuery("#obj-nav").removeClass("clicked");
jQuery("#acc-nav").removeClass("clicked");
});
jQuery('#art-nav').click(function(){
jQuery("#art-nav").addClass("clicked");
jQuery("#shop-nav").removeClass("clicked");
jQuery("#obj-nav").removeClass("clicked");
jQuery("#acc-nav").removeClass("clicked");
});
等等等等!
HTML 是
<div id="nav-cell-1" class="grid f-cell nav-cell">
<ul id="main-nav" class="nav clearfix">
<li><a href="#" id="shop-nav">Shop</a>
<ul id="shop-cats">
<li><a href="#" id="art-nav">Art</a></li>
<li>•</li>
<li><a href="#" id="obj-nav">Objects</a></li>
<li>•</li>
<li><a href="#" id="acc-nav">Accessories</a></li>
</ul>
</li>
</ul>
</div>
CSS:
a:link, a:visited {color:#cfb199;text-decoration:none} /* official this color:#9d9fa1; work color: #222*/
a:active, a:hover {color:#9d9fa1;text-decoration:none} /* old color:#9d9fa1; */ /* official color:#cfb199; work color: #f00*/
a:link.clicked, a:visited.clicked {color:green;text-decoration:underline}
演示站点在这里: http://www.tomcarden.net/birdycitynav/partial-nav-demo.html
我确实通过使用 this
引用解决了部分问题,但这不包括 .removeClass
部分。
jQuery('#shop-cats>li>a').click(function(){
jQuery(this).addClass("clicked");
});
i have a Nav wherein i'm attempting to use jQuery's addClass method to set the link color of the last clicked link. problem is then i have to use removeClass on all other links in the Nav. that's what i'm having trouble with.
I have written the code in a naive way, but know this is not good programming. below is the code with style sheet ref.
jQuery('#shop-nav').click(function(){
jQuery("#shop-nav").addClass("clicked");
jQuery("#art-nav").removeClass("clicked");
jQuery("#obj-nav").removeClass("clicked");
jQuery("#acc-nav").removeClass("clicked");
});
jQuery('#art-nav').click(function(){
jQuery("#art-nav").addClass("clicked");
jQuery("#shop-nav").removeClass("clicked");
jQuery("#obj-nav").removeClass("clicked");
jQuery("#acc-nav").removeClass("clicked");
});
etc. etc!
the HTML is
<div id="nav-cell-1" class="grid f-cell nav-cell">
<ul id="main-nav" class="nav clearfix">
<li><a href="#" id="shop-nav">Shop</a>
<ul id="shop-cats">
<li><a href="#" id="art-nav">Art</a></li>
<li>•</li>
<li><a href="#" id="obj-nav">Objects</a></li>
<li>•</li>
<li><a href="#" id="acc-nav">Accessories</a></li>
</ul>
</li>
</ul>
</div>
CSS:
a:link, a:visited {color:#cfb199;text-decoration:none} /* official this color:#9d9fa1; work color: #222*/
a:active, a:hover {color:#9d9fa1;text-decoration:none} /* old color:#9d9fa1; */ /* official color:#cfb199; work color: #f00*/
a:link.clicked, a:visited.clicked {color:green;text-decoration:underline}
a demo site is here:
http://www.tomcarden.net/birdycitynav/partial-nav-demo.html
I did solve part of the problem by using the this
reference, but this do not include the .removeClass
part.
jQuery('#shop-cats>li>a').click(function(){
jQuery(this).addClass("clicked");
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
或者这个更像你的网站:
在这里测试一下:
http://www.jsfiddle.net/mjYq3/18/
Or this one works more like your site:
test it here:
http://www.jsfiddle.net/mjYq3/18/
试试这个来切换班级:
Try this to toggle the class:
未经测试
Untested
您可以先删除所有单击的类,然后将其添加回仅单击的类。
You can first remove all the clicked classes then add it back to just the one that was clicked.
这应该工作:
基于您显然希望对
#main-nav
的后代的所有链接执行此操作,而不仅仅是内部<中的链接code>列表。
This should work:
On the basis that you apparently want to do this for all links that are descendents of
#main-nav
, and not just those that are in the inner<ul>
list.