Given an anchor element (with something like $("a:first")
), how do you get the absolute URL that the anchor points to?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
If you're using jQuery 1.6+, you can use .prop()
:
$("a:first").prop("href")
Prior to 1.6, you can access the href
property directly on the DOM element:
$("a:first")[0].href;
回答2:
to get the URL attached you can do something like...
var url = $("a:first").attr('href');
this will give you the URL but doesnt guarantee absolute or relative.
To find the absolute URL you can further check
if(!url.startsWith("http")) { url = "http://www.mysite.com" + url}
回答3:
var x = "http://lol.com/" + $("a:first").prop('href');
that should work unless it's an external url :)