我有一个简单的jQuery脚本,我试图在其上构建,但我无法使href字符串比较返回true:
<a class="test" href="/Services/Cloud-Hosting">Click Me</a>我的脚本如下:
$('.test').click(function() {
if ($(this).href == "/Services/Cloud-Hosting") {
alert('hi');
}
else {
alert('no');
}
});即使hrefs是一样的,我也一直收到“no”的警告。我遗漏了什么?
发布于 2012-07-03 02:14:51
更改:
if ($(this).href至:
if (this.href或者$(this).attr('href'),但前者更好。
要读取属性,您需要使用attr (attribute的简写)
这是你应该拥有的:
if (this.href == "/Services/Cloud-Hosting") {
alert('hi');
}
else {
alert('no');
}发布于 2012-07-03 02:15:44
试试这个:
if ($(this).attr('href') == "/Services/Cloud-Hosting") {发布于 2012-07-03 02:15:28
查看.attr()并尝试如下所示:
$(this).attr('href') == "/Services/Cloud-Hosting"相反,
https://stackoverflow.com/questions/11298953
复制相似问题