Getting an anchor element's absolute URL with jQuery

断了今生、忘了曾经 提交于 2019-12-03 23:24:32

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;

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}

var x = "http://lol.com/" + $("a:first").prop('href');

that should work unless it's an external url :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!