开发者

Getting href attribute as written with javascript

开发者 https://www.devze.com 2023-03-21 17:50 出处:网络
I would like to get an href attribute as it appears in the html - instead of a fully qualified url. <a href=\"foo.html\">foo</a>

I would like to get an href attribute as it appears in the html - instead of a fully qualified url.

<a href="foo.html">foo</a>

$('a[href]').each(function(){
   alert(this.href);
});

Gets an absolute url starting with htt开发者_运维问答p:// . I'd like it to get "foo.html" instead.


Try .attr('href') to get the href value.


You will need to alter the way you loop through the element, and the way you use this:

$('a').each(function(){
   alert( $( this ).attr( 'href' ) );
});

Working example here.

0

精彩评论

暂无评论...
验证码 换一张
取 消