单击导航添加类/删除类
我有一个菜单,链接到同一页面上的内容,并通过 jQuery 淡入淡出 div,工作完美。
导航中的第一个链接附加了一个类(current_page_item),我希望在单击导航中的任何其他链接时删除该类并将其添加到单击的链接中。
到目前为止,我已经尝试过类似的方法:
$(function() {
$("#nav-links a").click(function(){
$("#nav-links ul li").removeClass("current_page_item").find("li:last a").addClass("current_page_item");
});
});
这不起作用。
我对 javascript 很陌生,因此我们将不胜感激。
谢谢!
编辑:
这是导航的 HTML:
<div id="nav-wrap">
<ul class="group" id="nav-links">
<li class="current_page_item"><a href="#recent" class="linkclass recent">RECENT</a></li>
<li><a href="#work" class="linkclass work">WORK</a></li>
<li><a href="#who" class="linkclass who">WHO</a></li>
<li><a href="#services" class="linkclass services">SERVICES</a></li>
<li><a href="#contact" class="linkclass contact">CONTACT</a></li>
</ul>
</div>
编辑 2:
使用下面的答案似乎仍然没有达到我想要的效果。也许是因为我正在使用 jQuery 插件(魔线)。 链接
所以基本上我希望“线”留在下面点击。如果我在正确的位置加载一个带有“current_page_item”的新页面,那么它就可以工作,但是当我淡入和淡出 DIV 时,我无法做到这一点。
该行反弹回具有“current_page_item”类的 li 项目。也许可以修改这段代码以将类添加到单击的元素中?
这是代码。
$(function() {
var $el, leftPos, newWidth,
$mainNav = $("#nav-links");
$mainNav.append("<li id='magic-line'></li>");
var $magicLine = $("#magic-line");
$magicLine
.width($(".current_page_item").width())
.css("left", $(".current_page_item a").position().left)
.data("origLeft", $magicLine.position().left)
.data("origWidth", $magicLine.width());
$("#nav-links li a").hover(function() {
$el = $(this);
leftPos = $el.position().left;
newWidth = $el.parent().width();
$magicLine.stop().animate({
left: leftPos,
width: newWidth
});
}, function() {
$magicLine.stop().animate({
left: $magicLine.data("origLeft"),
width: $magicLine.data("origWidth")
});
});
});
I have a menu that links to content on the same page and through jQuery fades div's in and out, working perfectly.
The first link in the navigation has a class attached to it (current_page_item) and I would like, on click of any other link in the navigation, to remove that class and add it to the clicked link.
So far I've tried something like:
$(function() {
$("#nav-links a").click(function(){
$("#nav-links ul li").removeClass("current_page_item").find("li:last a").addClass("current_page_item");
});
});
which doesn't work.
I'm very new to javascript so any help would be appreciated.
Thanks!
EDIT:
Here is the HTML for the navigation:
<div id="nav-wrap">
<ul class="group" id="nav-links">
<li class="current_page_item"><a href="#recent" class="linkclass recent">RECENT</a></li>
<li><a href="#work" class="linkclass work">WORK</a></li>
<li><a href="#who" class="linkclass who">WHO</a></li>
<li><a href="#services" class="linkclass services">SERVICES</a></li>
<li><a href="#contact" class="linkclass contact">CONTACT</a></li>
</ul>
</div>
EDIT 2:
Using the answers below it still doesn't seem to achieve what I want. Perhaps it's because of the jQuery plugin I'm using (magic line). Link
So basically I'd like the 'line' to stay underneath what is clicked. Which works if I load a new page with the 'current_page_item' in the right place, but as I'm fading DIVs in and out, I can't do that.
The line bounces back to the li item with 'current_page_item' class. Perhaps this code can be modified to add the class to the clicked element?
Here's the code.
$(function() {
var $el, leftPos, newWidth,
$mainNav = $("#nav-links");
$mainNav.append("<li id='magic-line'></li>");
var $magicLine = $("#magic-line");
$magicLine
.width($(".current_page_item").width())
.css("left", $(".current_page_item a").position().left)
.data("origLeft", $magicLine.position().left)
.data("origWidth", $magicLine.width());
$("#nav-links li a").hover(function() {
$el = $(this);
leftPos = $el.position().left;
newWidth = $el.parent().width();
$magicLine.stop().animate({
left: leftPos,
width: newWidth
});
}, function() {
$magicLine.stop().animate({
left: $magicLine.data("origLeft"),
width: $magicLine.data("origWidth")
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
$(this)
访问被点击的元素,并使用parent()
访问其父li
元素以添加current_page_item
代码>类。下面是最终代码,您可以在此处找到一个工作示例。You can reach clicked element with
$(this)
and access to it's parentli
element withparent()
to addcurrent_page_item
class. Below is the final code and you can find a working example here.试试这个
try this
nav-links
是一个ul
,因此$("#nav-links ul li")
表示所有li
在
ul
女巫中是nav-links
的后代,显然没有。您必须使用
$("#nav-links li")
。.find
搜索所有后代,而这不是您要查找的内容为了。在.find
之前使用.closest("ul")
希望有帮助。
nav-links
is aul
so$("#nav-links ul li")
means allli
swithin a
ul
witch is a descendant ofnav-links
and obviouslythere's none. You have to use
$("#nav-links li")
..find
searches for all descendents and that's not what you're looking for. Use.closest("ul")
just before.find
Hope it helps.
.find() 只查找后代元素。您正在 li 上执行 .find ,该 li 是您正在搜索的元素的同级元素。
.find() only finds decedent elements. you are doing .find on an li which is the sibling of the element you are searching for.