如何获得点击链接的文本值? [英] How to get the text value of a clicked link?

查看:75
本文介绍了如何获得点击链接的文本值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文档的不同部分都有匹配的文本.第一个是像这样的表中的一组标签":

I have matching text in different parts of a document. The first is a set of "tags" in a table like so:

<div id="my-div">
  <div><a href="#">tag 1</a></div>
  <div><a href="#">tag 2</a></div>
</div>

然后在文档的其他几个部分中,当要选择匹配的链接时,在要突出显示的项目后面有一个隐藏元素,如下所示:

Then in several other parts of the document, I have a hidden element after the items I want to highlight when the matching link is selected like so:

<div class="hide-me">tag 1</div>

然后我的点击功能是这样的:

Then my click function is like this:

$('#my-div a').click(function() {
  var txt = $(this).text();
  console.log(txt);
});

输出是一个空字符串,但是我不确定为什么.

The output is an empty string, but I’m not sure why.

推荐答案

您的代码似乎正确,也可以尝试使用此代码.

your code seems to be correct, try this one too.

$('#my-div a').click(function(e) {
  var txt = $(e.target).text();
  console.log(txt);
});

这篇关于如何获得点击链接的文本值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆