gpt4 book ai didi

jquery - 从 anchor 标记获取文本

转载 作者:行者123 更新时间:2023-12-03 21:29:10 25 4
gpt4 key购买 nike

我有以下 anchor 标记

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

我知道如何从 anchor 获取 href:

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

但是如何从 anchor 标记中获取文本,即如何获取“Google”?

最佳答案

使用.text()为此:

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

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

alert($(this).html());

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

<a href="http://www.google.com/">Google <span>(External)</span></a>

然后就会有:

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

关于jquery - 从 anchor 标记获取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3871228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com