代码之家  ›  专栏  ›  技术社区  ›  oshirowanen

从定位标记获取文本

  •  32
  • oshirowanen  · 技术社区  · 14 年前

    我有下面的锚标签

    <a href="http://www.google.com/">Google</a>
    

    我知道如何从锚上获取Href:

    alert($(this).attr("href"));
    

    但是如何从锚标签中获取文本,即如何获取“google”?

    2 回复  |  直到 7 年前
        1
  •  70
  •   Nick Craver    14 年前

    .text()

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

    .html()

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

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

    $(this).text() //"Google (External)"
    $(this).html() //"Google <span>(External)</span>"
    
        2
  •  0
  •   Nirav Joshi    7 年前

    <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> 
    

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