Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery this.href字符串比较不起作用_Javascript_Jquery_This_Href - Fatal编程技术网

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

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.

我有一个简单的jQuery脚本,我正试图以此为基础构建它,但我无法让href字符串比较返回true:

<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')
吗?