Getting an anchor element's absolute URL with jQuery

后端 未结 3 1915
隐瞒了意图╮
隐瞒了意图╮ 2021-02-19 05:12

Given an anchor element (with something like $(\"a:first\")), how do you get the absolute URL that the anchor points to?

3条回答
  •  眼角桃花
    2021-02-19 05:44

    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}

提交回复
热议问题