Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CSS Jquery-活动链接_Jquery_Html_Css - Fatal编程技术网

CSS Jquery-活动链接

CSS Jquery-活动链接,jquery,html,css,Jquery,Html,Css,有人能看出我做错了什么吗?试图让一个活跃的状态工作,我真的看不出我做错了什么 剧本 你应该使用 if (href === window.location.href) { 而不是 if (href === window.location.pathname) { 如果您想找到特定问题中所示的具有锚定链接的活动状态,那么它将不适用于window.location.pathname,因为它只在域名后提供路径名,而不在页面中提供锚定链接。您必须使用window.location.href,但即使这样,

有人能看出我做错了什么吗?试图让一个活跃的状态工作,我真的看不出我做错了什么

剧本

你应该使用

if (href === window.location.href) {
而不是

if (href === window.location.pathname) {

如果您想找到特定问题中所示的具有锚定链接的活动状态,那么它将不适用于window.location.pathname,因为它只在域名后提供路径名,而不在页面中提供锚定链接。您必须使用window.location.href,但即使这样,也会提供完整的链接,而您只能从href属性中获得后面的部分。我会用。看起来是这样的

//Matching if href ever occurs in windows.location.href
if (window.location.href.match(href)){
//do something
}

另外,因为match实际上接受一个regex,所以它实际上返回匹配的字符串,如果不匹配,则返回null。您可以更具体地在末尾添加一个$,用于检查字符串的结尾,以查看链接是否以该锚链接结尾

仍然不起作用。我试过IE9和Chrome。使用我包含的最新jquery。jquery-1.11.1.min。js@user263029您可能希望检查window.location.href是否包含您的.attr'href'而不是===.Hm,尝试添加console.loghref、window.location.href,查看您正在竞争什么,并使用==而不是off==进行比较只需单击任何链接,并检查地址栏中的地址是否正在更改。当然,不像那些链接到同一页面上的元素那样,所以window.location.pathname不起作用了//
if (href === window.location.href) {
if (href === window.location.pathname) {
//Matching if href ever occurs in windows.location.href
if (window.location.href.match(href)){
//do something
}