how can i get the text inside an anchor tag in jquery

落爺英雄遲暮 提交于 2019-12-19 09:05:56

问题


<div id="div1">
<a herf="#">link1</a>
<a href="#">link2</a>
</div>

//jquery
$("#div1 a").click(function(){
var text = $("#div1 a").text();
});

on the above tags i want to get text in side an anchor tag which i clicked on it but clicking on each of above anchor tag gives me a same answer ('link1link2'), how can i get 'link1' when i click on first a tag and get 'link2' when i click on second anchor tag, be aware that anchor tags can be infinite


回答1:


Use this :

  $("#div1 a").click(function(){
    var text = $(this).text();
  });



回答2:


Inside the click handler, this refers to the element which triggered the handler execution :

$("#div1 a").click(function(){
    var text = $(this).text();
});


来源:https://stackoverflow.com/questions/18823065/how-can-i-get-the-text-inside-an-anchor-tag-in-jquery

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!