[英]How do I get at the text inside this anchor tag with jQuery?
If I have a link like this: 如果我有这样的链接:
<ul id="main">
<li>
<a href="/login">Sign In</a>
</li>
</ul>
How do I retrieve the text Sign In
with jQuery? 如何使用jQuery检索文本Sign In
?
Use the text
method. 使用text
方法。 Example: 例:
var txt = $('#main li a').text();
This is useful for grabbing the text based on the hyperlink URI: 这对于基于超链接URI获取文本很有用:
var linkText = $('a[href="/login"]').text();
http://api.jquery.com/attribute-equals-selector/ http://api.jquery.com/attribute-equals-selector/
If that's actually the only text, you can get it direclty from the #main
element. 如果那是实际上唯一的文本,你可以从#main
元素中获取它。 No need to drill down to the <a>
element. 无需深入查看<a>
元素。
$('#main').text();
Though you may want to trim it. 虽然你可能想修剪它。
$.trim( $('#main').text() );
Working Example: http://jsfiddle.net/GqKaN/ 工作示例: http : //jsfiddle.net/GqKaN/
You can use this approach, which selects the anchor by its href attribute: 您可以使用此方法,通过其href属性选择锚点:
$("#main a[href=/login]").text()
http://api.jquery.com/attribute-equals-selector/ http://api.jquery.com/attribute-equals-selector/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.