Get href attribute on jQuery

后端 未结 6 1411
眼角桃花
眼角桃花 2021-02-12 13:24

I have some table rows


    
        
                      
相关标签:
6条回答
  • 2021-02-12 13:51

    Use this:

    $(function(){
        $("tr.b_row").each(function(){
        var a_href = $(this).find('div.cpt h2 a').attr('href');
        alert ("Href is: "+a_href);
    
        });
    });
    

    See a working demo: http://jsfiddle.net/usmanhalalit/4Ea4k/1/

    0 讨论(0)
  • 2021-02-12 13:53

    In loop you should refer to the current procceded element, so write:

    var a_href = $(this).find('div.cpt h2 a').attr('href');
    
    0 讨论(0)
  • 2021-02-12 13:57

    Very simply, use this as the context: http://api.jquery.com/jQuery/#selector-context

    var a_href = $('div.cpt', this).find('h2 a').attr('href');
    

    Which says, find 'div.cpt' only inside this

    0 讨论(0)
  • 2021-02-12 14:02

    add a reference to this, which refers to your b_row:

    $("tr.b_row").each(function(){
        var a_href = $( this ).find('div.cpt h2 a').attr('href');
        alert ("Href is: "+a_href);
    });
    
    0 讨论(0)
  • 2021-02-12 14:04

    Use $(this) for get the desire element.

    function openAll()
    {
         $("tr.b_row").each(function(){
            var a_href = $(this).find('.cpt h2 a').attr('href');
            alert ("Href is: "+a_href);
         });
    }
    
    0 讨论(0)
  • 2021-02-12 14:09
    var a_href = $('div.cpt').find('h2 a').attr('href');
    

    should be

    var a_href = $(this).find('div.cpt').find('h2 a').attr('href');
    

    In the first line, your query searches the entire document. In the second, the query starts from your tr element and only gets the element underneath it. (You can combine the finds if you like, I left them separate to illustrate the point.)

    0 讨论(0)
提交回复
热议问题