gpt4 book ai didi

javascript - 在没有 preventDefault() 的情况下更改链接颜色 onclick

转载 作者:行者123 更新时间:2023-11-28 00:11:28 25 4
gpt4 key购买 nike

如果单击,我需要更改链接的颜色。

如果我使用 event.preventDefault(); 永久应用颜色但链接不起作用,则 uri 未通过,然后我无法使用 $_GET ['route'] 因为我使用 mod_rewrite 将所有 uri 重定向到这个 $_GET 变量。 ^(.*)$index.php?route=$1

如果我不使用 event.preventDefault 链接有效并且部分发生变化,但 addClass 仅在我单击时应用,然后消失...

如何获得这两种行为?能够传递 URI (HREF),并永久改变颜色 onClick ?

html:

<a href="./section">Section</a>

CSS:

.active { color: #f00; }

J查询:

$('a').on('click', function(event) {
//event.preventDefault
$(this).addClass("active");
});

最佳答案

您可以在 doc ready 中尝试此操作,无需指定任何 .click() 事件:

试试 fiddle :http://jsfiddle.net/cmwt8/

$(function(){
var url = window.location.href; // url should be this way 'http://aaa.com/page/section'
var link = url.substr(url.lastIndexOf('/') + 1); // then you get here 'section'
$('[href*="' + link + '"]').addClass("active"); // if found add the class
});

关于javascript - 在没有 preventDefault() 的情况下更改链接颜色 onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14929985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com