使用jQuery,如何获取元素的文本值?

时间:2022-11-27 07:58:31

how do I retrieve the text between the href tag?

如何检索href标签之间的文本?

<a href="blah">GET THIS TEXT</a>

It is wrapped in this DOM:

它包含在这个DOM中:

<div class="c1">
   <div class="c2"><a href="#">GET THIS TEXT</a>
   </div>
</div>

2 个解决方案

#1


You are looking for the text() property:

您正在寻找text()属性:

$('a', 'div.c2').text();

The reason div.c2 is defined as the second argument is because it would then be the context of the selector. By default jQuery searches the entire document when you use $(), by specifying div.c2 it is only going to search inside of <div>'s with a class of c2.

将div.c2定义为第二个参数的原因是因为它将成为选择器的上下文。默认情况下,当您使用$()时,jQuery会搜索整个文档,通过指定div.c2,它只会在类别为c2的

中搜索。

Keeping this in mind, you could also rewrite the above like this:

记住这一点,你也可以像这样重写上面的内容:

$('div.c2 a').text();

Most people prefer this because it is the syntax that you would use to select this element in a CSS stylesheet. However, jQuery then has to figure that out. You could even do:

大多数人更喜欢这个,因为它是用于在CSS样式表中选择此元素的语法。但是,jQuery必须弄明白这一点。你甚至可以这样做:

$('div.c2').find('a').text();

However, I prefer the context method as I feel it is cleaner and marginally faster, not that it matters.

但是,我更喜欢上下文方法,因为我觉得它更干净,速度更快,而不是重要。

There is also the html() property, which gets the contents, including HTML.

还有html()属性,它获取内容,包括HTML。

So if your link was like this:

所以,如果您的链接是这样的:

<a href='#'><b>Hi There</b></a>

Then:

$('a').text(); // would return Hi There
$('a').html(); // would return <b>Hi There</b>

#2


I think that .text() or .html() will work, depending on whether you don't or do want any markup that appears inside the tag.

我认为.text()或.html()将起作用,具体取决于你是否不想或者想要在标签内出现任何标记。

var txt = $('div.c2 > a').text();

var html = $('div.c2 > a').html()

#1


You are looking for the text() property:

您正在寻找text()属性:

$('a', 'div.c2').text();

The reason div.c2 is defined as the second argument is because it would then be the context of the selector. By default jQuery searches the entire document when you use $(), by specifying div.c2 it is only going to search inside of <div>'s with a class of c2.

将div.c2定义为第二个参数的原因是因为它将成为选择器的上下文。默认情况下,当您使用$()时,jQuery会搜索整个文档,通过指定div.c2,它只会在类别为c2的

中搜索。

Keeping this in mind, you could also rewrite the above like this:

记住这一点,你也可以像这样重写上面的内容:

$('div.c2 a').text();

Most people prefer this because it is the syntax that you would use to select this element in a CSS stylesheet. However, jQuery then has to figure that out. You could even do:

大多数人更喜欢这个,因为它是用于在CSS样式表中选择此元素的语法。但是,jQuery必须弄明白这一点。你甚至可以这样做:

$('div.c2').find('a').text();

However, I prefer the context method as I feel it is cleaner and marginally faster, not that it matters.

但是,我更喜欢上下文方法,因为我觉得它更干净,速度更快,而不是重要。

There is also the html() property, which gets the contents, including HTML.

还有html()属性,它获取内容,包括HTML。

So if your link was like this:

所以,如果您的链接是这样的:

<a href='#'><b>Hi There</b></a>

Then:

$('a').text(); // would return Hi There
$('a').html(); // would return <b>Hi There</b>

#2


I think that .text() or .html() will work, depending on whether you don't or do want any markup that appears inside the tag.

我认为.text()或.html()将起作用,具体取决于你是否不想或者想要在标签内出现任何标记。

var txt = $('div.c2 > a').text();

var html = $('div.c2 > a').html()