Get text from anchor tag

后端 未结 2 1221
死守一世寂寞
死守一世寂寞 2020-12-24 05:51

I have the following anchor tag

Google

I know how to get the href from an anchor take:

相关标签:
2条回答
  • 2020-12-24 06:04

    On using the .text() as mentioned, I got the text of all the anchor tags combined in my code:

    HTML:

    <div class="col-sm-2 jumbotron jumbotronUserRaces list-group list-group-
    userRaces" id="listGroupUserRaces">
                    <a href="" class="list-group-item active">First item</a>
                    <a href="" class="list-group-item">Second item</a>
                    <a href="" class="list-group-item">Third item</a>
                </div> 
    

    JS:

    $("#listGroupUserRaces").click(function () {
       alert($(this).text());
    });
    

    OUTPUT:

    • First item
    • Second item
    • Third item
    0 讨论(0)
  • 2020-12-24 06:06

    Use .text() for this:

    alert($(this).text());
    

    If you wanted the markup (.text() removes tags and such), use .html()

    alert($(this).html());
    

    In this case there's no difference, if instead you had this:

    <a href="http://www.google.com/">Google <span>(External)</span></a>
    

    Then there would be:

    $(this).text() //"Google (External)"
    $(this).html() //"Google <span>(External)</span>"
    
    0 讨论(0)
提交回复
热议问题