gpt4 book ai didi

当用户打开特定的 Url 时,Javascript 插入 css 类

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

当用户打开页面:contact.html

我想在 .sidebar 类中插入类:.contact

当用户打开页面:products.html

我想在 .sidebar 类中插入类:.products

html:

<div class="sidebar">


</div>

js:

 <script type="text/javascript">
$(function() {
var loc = window.location.href; // returns the full URL
if(/contact/.html(loc)) {
$('.sidebar').addClass('contact');
}
});
</script>

最佳答案

在正则表达式中使用 .test(loc) 而不是 .html(loc)。喜欢:

$(function() {
var loc = window.location.href; // returns the full URL
if(/contact/.test(loc)) {
$('.sidebar').addClass('contact');
}
});

希望这对您有所帮助!

关于当用户打开特定的 Url 时,Javascript 插入 css 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41000752/

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