jQuery this.href 字符串比较不起作用
jQuery this.href String Comparison Not Working
我有一个简单的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 是相同的。我错过了什么?
更改:
if ($(this).href
自:
if (this.href
或者$(this).attr('href')
但前者更好。
要读取属性,您需要使用 attr
(attribute
的简写)
这是你应该拥有的:
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"
相反
相关文章:
- 替换函数后的Javascript比较字符串不起作用
- 字符串比较在d3.rgb中不起作用
- 比较应用程序脚本中的两个数组不起作用
- Javascript 比较时间不起作用
- jQuery this.href 字符串比较不起作用
- 字符串比较不起作用
- Javascript数字变量比较不起作用
- Javascript中的字符串比较不起作用
- d3 本地存储比较数组 .filter 咖啡脚本;数组变量不起作用
- j查询帖子数据返回比较不起作用
- 将数组从文档属性与数据范围进行比较不起作用
- JavaScript 比较运算符不起作用
- 为什么我的比较不起作用
- Angular JS===比较不起作用
- 与-1或未定义进行比较不起作用,无论是使用==还是===
- Javascript字符串比较不起作用
- 在$filter应用于3位数的负数之后,小于比较不起作用
- dhtmlxform.发送的响应在警告提示中正确显示,但字符串比较不起作用
- 或者比较不起作用
- Javascript风格比较不起作用