jQuery this.href字符串比较不起作用 [英] jQuery this.href String Comparison Not Working

查看:233
本文介绍了jQuery this.href字符串比较不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个尝试构建的简单jQuery脚本,但无法获得href字符串比较以返回true:

I have a simple jQuery script that I'm trying to build upon but I can't get the href string comparison to return 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相同,我也会不断收到否"的警报.我想念什么?

I keep getting the alert of 'no' even thought the hrefs are the same. What am I missing?

推荐答案

更改:

if ($(this).href

收件人:

if (this.href

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

要读取属性,您需要使用attr(attribute的简写)

To read attributes, you need to use attr (shorthand for attribute)

这是您应该拥有的:

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

这篇关于jQuery this.href字符串比较不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆