Javascript 使用页面跳转(单页网站)时如何更改css class=“current”?
早上好 我一定是问了谷歌所有错误的问题,因为我找不到类似的问题 我有一个标准的导航列表,但我使用页面跳转,因为我想要一个单独的网页Javascript 使用页面跳转(单页网站)时如何更改css class=“current”?,javascript,jquery,css,Javascript,Jquery,Css,早上好 我一定是问了谷歌所有错误的问题,因为我找不到类似的问题 我有一个标准的导航列表,但我使用页面跳转,因为我想要一个单独的网页 <ul> <li><a href="#livestream">Livestream</a></li> <li><a href="#media">Media</a></li> <li><a href="#crew">Crew</a&
<ul>
<li><a href="#livestream">Livestream</a></li>
<li><a href="#media">Media</a></li>
<li><a href="#crew">Crew</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#contact">Contact</a></li>
</ul>
但我一辈子都搞不懂如何在使用页面跳转时使class=current。我试过了,因为它似乎就是我要找的,但它什么也没做。我认为它对链接不起作用
有什么想法吗?你想让class=current打开什么 将其应用于点击表单并删除所有其他链接的逻辑:
$('a').click(function(){
//remove from other links, they're no longer current
$('a').removeClass('current');
//this is now the current active link.
$(this).addClass('current');
});
我相信这就是你要问的。如果您有任何问题,请告诉我。您想让class=current打开什么 将其应用于点击表单并删除所有其他链接的逻辑:
$('a').click(function(){
//remove from other links, they're no longer current
$('a').removeClass('current');
//this is now the current active link.
$(this).addClass('current');
});
我相信这就是你要问的。如果您有任何问题,请告诉我。如果您想将当前类添加到已单击的a中,您可以这样完成:
// Wait for the DOM to be ready
$(function(){
// Add click-event listener
$("li a").click(function(){
// Remove the current class from all a tags
$("li a").removeClass("current");
// Add the current class to the clicked a
$(this).addClass("current");
});
});
如果要将当前类添加到已单击的a中,可以按如下方式完成:
// Wait for the DOM to be ready
$(function(){
// Add click-event listener
$("li a").click(function(){
// Remove the current class from all a tags
$("li a").removeClass("current");
// Add the current class to the clicked a
$(this).addClass("current");
});
});
在我们讨论锚定时,您应该执行.preventDefault以确保不触发锚定默认操作
在我们讨论锚定时,您应该执行.preventDefault以确保不触发锚定默认操作
你的意思是如何使类=当前?是否要将类添加到已单击的链接中?如何将类设置为当前类?是否要将当前类添加到单击的链接?谢谢!现在,这意味着从所有a标记中删除当前类是什么?我不想把整页都撕了。添加$.header navi li a.clickfunction是否正确?我已经添加了它,它不会破坏任何东西,但这是正确的吗?当然,这会起作用。我只是不知道你剩下的DOM是什么样子。您的selector.header navi li a将仅选择一个标记,这些标记是li的子级,而li又必须是具有类header navi的元素的子级。在这种情况下,您也应该对$.header navi li a.removeClasscurrent使用相同的选择器。谢谢!现在,这意味着从所有a标记中删除当前类是什么?我不想把整页都撕了。添加$.header navi li a.clickfunction是否正确?我已经添加了它,它不会破坏任何东西,但这是正确的吗?当然,这会起作用。我只是不知道你剩下的DOM是什么样子。您的selector.header navi li a将仅选择一个标记,这些标记是li的子级,而li又必须是具有类header navi的元素的子级。在这种情况下,还应该对$.header navi li a.removeClasscurrent使用相同的选择器。