javascript 如何使用 jQuery 获取链接文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22439263/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:09:31  来源:igfitidea点击:

How to get link text using jQuery?

javascriptjquery

提问by user3000967

How can I get link text using jQuery?

如何使用 jQuery 获取链接文本?

My link is:

我的链接是:

<a href="htp://www.google.com" id="id1">google site</a>

I need to get "google site" using jQuery.

我需要使用 jQuery 获取“谷歌网站”。

回答by Afzaal Ahmad Zeeshan

That is the text shown to the user in UI not the name. To get that you use a jQuery's text method.

那是在 UI 中向用户显示的文本,而不是名称。为此,您可以使用 jQuery 的 text 方法。

Use the .text()method as

使用该.text()方法作为

$('a').text();

Even you can use a variable to get the value to, for example:

即使您可以使用变量来获取值,例如:

var hyperLinkText = $('a').text(); // get the name
$('element').html(hyperLinkText); // write it somewhere

回答by Felix

If you means the hrefvalue of your anchor, then you can use .attr():

如果您的意思是href锚的价值,那么您可以使用.attr()

var href = $('#id1').attr('href');

or .prop():

.prop()

var href = $('#id1').prop('href');

If you want to get the text inside anchor, you can use .text():

如果你想获得锚内的文本,你可以使用.text()

var text = $('#id1').text();

回答by Mubin

you can try with this:

你可以试试这个:

var linkText=$('#id1').text();