jQuery this.href字符串比较不起作用

我有一个简单的jQuery脚本,我正在尝试构建,但我不能让href字符串比较返回true:

Click Me 

我的脚本如下:

 $('.test').click(function() { if ($(this).href == "https://stackoverflow.com/Services/Cloud-Hosting") { alert('hi'); } else { alert('no'); } });​ 

即使href是相同的,我仍然会收到’不’的警报。 我错过了什么?

更改:

 if ($(this).href 

至:

 if (this.href 

$(this).attr('href')但前者更好。

要读取属性,需要使用attrattribute简写)

这是你应该拥有的:

 if (this.href == "/Services/Cloud-Hosting") { alert('hi'); } else { alert('no'); } 

试试这个:

 if ($(this).attr('href') == "/Services/Cloud-Hosting") { 

jQuery对象没有href属性。 只需使用this.href访问HTMLAnchorElement的属性,而不是使用$(this)创建一个新的jQuery对象。

请参阅.attr()并尝试以下操作:

 $(this).attr('href') == "/Services/Cloud-Hosting" 

代替