从锚标签获取文本 [英] Get text from anchor tag

查看:71
本文介绍了从锚标签获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下锚标签

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

我知道如何从主播获取href:

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

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

但是如何从锚标记获取文本,即如何获取"Google"?

But how do I get the text from the anchor tag, i.e. how do I get "Google"?

推荐答案

使用 .text() 这个:

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

如果您想要标记( .text() 会删除标签等),请使用 .html()

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>

然后将是:

$(this).text() //"Google (External)"
$(this).html() //"Google <span>(External)</span>"

这篇关于从锚标签获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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