Javascript this.href vs $(this).attr('href')

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6977049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:05:27  来源:igfitidea点击:

this.href vs $(this).attr('href')

javascriptjquery

提问by user

After reading this article net.tutsplus.com/tutorials/javascript-ajax/14-helpful-jquery-tricks-notes-and-best-practices/I came to conclusion that using this.hrefis more efficient.

阅读这篇文章net.tutsplus.com/tutorials/javascript-ajax/14-helpful-jquery-tricks-notes-and-best-practices/ 后,我得出结论,使用this.href更有效。

However, when I tried to use it on one of my projects, I saw that this.hrefreturns not only href but also appends a url of a website. For example <a href="tab-04"></a>this.hrefwill return http://example.com/abc/tab-04and $(this).attr('href')will return only tab-04.

但是,当我尝试在我的一个项目中使用它时,我看到this.href不仅返回 href 还附加了一个网站的 url。例如<a href="tab-04"></a>this.href将返回http://example.com/abc/tab-04$(this).attr('href')将只返回 tab-04。

You can see an example here http://jsfiddle.net/UC2xA/1/.

你可以在这里看到一个例子http://jsfiddle.net/UC2xA/1/

$(this).attr('href')however returns exactly what I need and nothing more.

$(this).attr('href')然而返回正是我需要的,仅此而已。

My question is this, how can I rewrite (or do what is necessary) this.hrefso that it would only return tab-04?

我的问题是,我怎样才能重写(或做必要的事情)this.href以便它只返回tab-04

EDIT

编辑

Doug you are right on the money with

道格,你是对的

this.getAttribute('href')

回答by Doug

The hrefproperty in plain Javascript will have the semantic attached to it. It returns the destination URL which the link will lead to. It doesn't matter how it was written (absolute or relative URLs).

href纯 Javascript 中的属性将附加语义。它返回链接将指向的目标 URL。它是如何编写的(绝对或相对 URL)并不重要。

When you use the $(this).attr("href")you are retrieving directly the value of hrefattribute just like any other attribute, so it will return the exact value rendered in the HTML.

当您使用 时,$(this).attr("href")href就像任何其他属性一样直接检索属性的值,因此它将返回在 HTML 中呈现的确切值。

For your case then, it's better to use $(this).attr("href")

那么对于你的情况,最好使用 $(this).attr("href")

If you don't want to use jQuery, there's yet another solution, using just plain JavaScript:

如果您不想使用 jQuery,还有另一种解决方案,只使用纯 JavaScript:

this.getAttribute('href')

回答by Brian

document.getElementById(yourAnchorId).href.split("/")

document.getElementById(yourAnchorId).href.split("/")

[document.getElementById(yourAnchorId).href.split("/").length - 1].split("?")[0];

[document.getElementById(yourAnchorId).href.split("/").length - 1].split("?")[0];

回答by ygaradon

what a but a little substring?

什么只是一个小子串?

for example :

例如 :

function getHref(a)
{
    var i = a.outerHTML.indexOf('href="')+6;
    return a.outerHTML.substring(i, a.outerHTML.indexOf('"', i));

}

P.S TESTED!

PS测试!