从锚标记中获取文本

osh*_*nen 36 jquery jquery-selectors

我有以下锚标记

<a href="http://www.google.com/">Google</a>
Run Code Online (Sandbox Code Playgroud)

我知道如何从锚点获取href:

alert($(this).attr("href"));
Run Code Online (Sandbox Code Playgroud)

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

Nic*_*ver 75

使用.text()此:

alert($(this).text());
Run Code Online (Sandbox Code Playgroud)

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

alert($(this).html());
Run Code Online (Sandbox Code Playgroud)

这种情况下没有区别,如果你有这个:

<a href="http://www.google.com/">Google <span>(External)</span></a>
Run Code Online (Sandbox Code Playgroud)

然后会有:

$(this).text() //"Google (External)"
$(this).html() //"Google <span>(External)</span>"
Run Code Online (Sandbox Code Playgroud)

  • 我怎么才能得到谷歌? (4认同)