Javascript jQuery this.href字符串比较不起作用
我有一个简单的jQuery脚本,我正试图以此为基础构建它,但我无法让href字符串比较返回true:Javascript jQuery this.href字符串比较不起作用,javascript,jquery,this,href,Javascript,Jquery,This,Href,我有一个简单的jQuery脚本,我正试图以此为基础构建它,但我无法让href字符串比较返回true: <a class="test" href="/Services/Cloud-Hosting">Click Me</a> 即使HREF是一样的,我也会不断收到“否”的警告。我缺少什么?更改: if ($(this).href 致: 或者$(this.attr('href'),但前者更好 要读取属性,需要使用attr(属性的缩写) 这是您应该拥有的: if (this.
<a class="test" href="/Services/Cloud-Hosting">Click Me</a>
即使HREF是一样的,我也会不断收到“否”的警告。我缺少什么?更改:
if ($(this).href
致:
或者$(this.attr('href')
,但前者更好
要读取属性,需要使用attr
(属性的缩写)
这是您应该拥有的:
if (this.href == "/Services/Cloud-Hosting") {
alert('hi');
}
else {
alert('no');
}
更改:
if ($(this).href
致:
或者$(this.attr('href')
,但前者更好
要读取属性,需要使用attr
(属性的缩写)
这是您应该拥有的:
if (this.href == "/Services/Cloud-Hosting") {
alert('hi');
}
else {
alert('no');
}
请查看并尝试以下操作:
$(this).attr('href') == "/Services/Cloud-Hosting"
相反,请参见并尝试以下内容:
$(this).attr('href') == "/Services/Cloud-Hosting"
相反,请尝试以下方法:
$(this).attr('href') == "/Services/Cloud-Hosting"
if ($(this).attr('href') == "/Services/Cloud-Hosting") {
试试这个:
$(this).attr('href') == "/Services/Cloud-Hosting"
if ($(this).attr('href') == "/Services/Cloud-Hosting") {
jQuery对象没有
href
属性。只需使用this.href
访问的属性,而不是使用$(this)
创建一个新的jQuery对象,jQuery对象没有href
属性。只需使用this.href
访问的属性,而不是使用$(this)
创建一个新的jQuery对象,在您得到答案后,立即检查此项了解解释,在您得到答案后,检查此项了解解释小心使用this.href
,在Chrome中,它返回链接的绝对路径(不管href
是相对URL)@DavidSaysRestatemonica该提示非常有价值,因此我认为它值得发布一个新的答案和该指导,以便研究人员更容易理解其中的差异。小心使用this.href
,在Chrome中,它返回链接的绝对路径(无论href
是否为相对URL)@DavidSaysRestatemonica:提示非常有价值,因此我认为有必要发布一个新的答案和该指导,以便研究人员更容易理解差异。正如另一个答案所指出的那样,this.href
不一定等于$(this.attr('href')
)@在这种情况下,DavidThomas不是首选this.getAttribute('href')
吗?正如另一个答案所指出的this.href
不一定等于$(this.attr('href')
)@在这种情况下,DavidThomas不是首选这个.getAttribute('href')
吗?