简体   繁体   English

如何在jquery中获取锚标记内的文本

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

<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 在上面的标签我希望得到一个锚点标签,我点击它,但点击上面的每个锚标签给我一个相同的答案('link1link2'),我怎么能得到'link1'当我点击第一当我点击第二个锚标记时,标记并获取“link2”,请注意锚标记可以是无限的

Use this : 用这个 :

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

Inside the click handler, this refers to the element which triggered the handler execution : 在click处理程序中, this指的是触发处理程序执行的元素:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM