开发者

accessing href attribute with classname

I want to access the href attribute with jquery using the class name. How can I do that below is the code.

<a href="开发者_Go百科www.google.com" class="class-name">Hyper Link</a>

I only want to access it with the class-name since I have many links and want to use the classname to access the href link.

Thanks


$('a.class-name').each(function(){
     this.href //do something with href
})


Presuming (1) that you are using jQuery 1.6 and (2) that your link is the only one that has that class:

var linkHref = $('a.class-name').prop('href');

If you are using jQuery 1.5 or older, you'll have to use attr rather than prop. If you have more than one element with the class, you'll have to find some other way of identifying which element you want.


Depends what you want - but the href of your link should probably be made into an absolute rather than relative link...

    $('a.class-name').each(function(){
        alert( this.href ) // alerts http://currentdomain.com/www.google.com
        alert( $(this).attr('href') ) // alerts www.google.com
    })


To make the element look up faster I'd suggest dropping the tag prefix since you mentioned you have a lot of links.

$('.class-name').prop('href');
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜