gpt4 book ai didi

javascript - 为什么 jquery 中的代码不起作用?

转载 作者:行者123 更新时间:2023-11-30 09:03:53 30 4
gpt4 key购买 nike

url是这样的:http://example.com/download/

var pathname = window.location.pathname;
if(pathname=='download/'){
$("#subnav-content div:first").hide();
$("#subnav-content div:second").show();
}

为什么上面的代码在 jquery 中不起作用?我想当 url 是 http://example.com/download/ .显示第二个分区。

ps*:这个检查会影响网站性能吗?*

最佳答案

您需要前导斜杠。

'/下载/'

如果您需要查询字符串参数,您可以尝试使用正则表达式来匹配 url 的下载部分:以下匹配/download/。

if (window.location.pathname.match(/^\/download\//i))

关于jquery,没有:second,你需要使用:eq(1)

var pathname = window.location.pathname;
if(pathname=='/download/'){
$("#subnav-content div:first").hide();
$("#subnav-content div:eq(1)").show();
}

回复评论

我将我的评论放在这里是因为评论中的格式很糟糕。匹配下载的正则表达式可以概括如下:

/ - 正则表达式匹配语法的开始
^ - 表示在屏幕的最开头开始匹配
\/ - 表示匹配文字字符串'/',这是一个必须转义的特殊字符
download - 匹配文字字符串“download”
\/ - 再次表示匹配文字字符串 '/'
/ - 匹配语法结束
i - 正则表达式选项,i 表示忽略大小写

我不清楚你的另一张纸条要求的是什么。

关于javascript - 为什么 jquery 中的代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6807265/

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