获取div元素的文本值(Getting text values of div elements)

我正在尝试获取所有div元素的文本值。

我的预期输出是:

Menu 1 Abc Def Menu 2 Ghi Jkl Menu 3 Mno Pqr

但我明白了

Menu 1 Abc Def Ghi Jkl Mno Pqr Menu 2 Abc Def ...

HTML

<div id="output"> <div> <input type="text" value="Menu 1"> <br /> <a href="#">Abc</a> <br /> <a href="#">Def</a> <br /> </div> <div> <input type="text" value="Menu 2"> <br /> <a href="#">Ghi</a> <br /> <a href="#">Jkl</a> </div> ... </div>

JS

$("#output div").each(function() { console.log($(this).find("input").val()); $("a").each(function() { console.log($(this).text() + "\n"); }); });

这是一个小提琴 。

I'm trying to get the text values of all of my div elements.

My expected output is:

Menu 1 Abc Def Menu 2 Ghi Jkl Menu 3 Mno Pqr

But I get

Menu 1 Abc Def Ghi Jkl Mno Pqr Menu 2 Abc Def ...

HTML

<div id="output"> <div> <input type="text" value="Menu 1"> <br /> <a href="#">Abc</a> <br /> <a href="#">Def</a> <br /> </div> <div> <input type="text" value="Menu 2"> <br /> <a href="#">Ghi</a> <br /> <a href="#">Jkl</a> </div> ... </div>

JS

$("#output div").each(function() { console.log($(this).find("input").val()); $("a").each(function() { console.log($(this).text() + "\n"); }); });

Here is a fiddle.

最满意答案

您必须为anchor选择器设置context ,

$("a", this).each(function() {

DEMO

上面的代码将选择上下文中存在a元素。 在我们的例子中, this将指向我们正在迭代的div 。 如果你使用没有任何上下文的$("a")那么它将选择整个DOM中不存在于特定元素内的所有元素。

You have to set the context for for the anchor selector,

$("a", this).each(function() {

DEMO

The above code will select the a elements present in the context this. Here in our case this will point to the div that we are iterating. If you use $("a") without any context then it will select all the a elements present in the entire DOM not inside a particular element.

获取div元素的文本值(Getting text values of div elements)

我正在尝试获取所有div元素的文本值。

我的预期输出是:

Menu 1 Abc Def Menu 2 Ghi Jkl Menu 3 Mno Pqr

但我明白了

Menu 1 Abc Def Ghi Jkl Mno Pqr Menu 2 Abc Def ...

HTML

<div id="output"> <div> <input type="text" value="Menu 1"> <br /> <a href="#">Abc</a> <br /> <a href="#">Def</a> <br /> </div> <div> <input type="text" value="Menu 2"> <br /> <a href="#">Ghi</a> <br /> <a href="#">Jkl</a> </div> ... </div>

JS

$("#output div").each(function() { console.log($(this).find("input").val()); $("a").each(function() { console.log($(this).text() + "\n"); }); });

这是一个小提琴 。

I'm trying to get the text values of all of my div elements.

My expected output is:

Menu 1 Abc Def Menu 2 Ghi Jkl Menu 3 Mno Pqr

But I get

Menu 1 Abc Def Ghi Jkl Mno Pqr Menu 2 Abc Def ...

HTML

<div id="output"> <div> <input type="text" value="Menu 1"> <br /> <a href="#">Abc</a> <br /> <a href="#">Def</a> <br /> </div> <div> <input type="text" value="Menu 2"> <br /> <a href="#">Ghi</a> <br /> <a href="#">Jkl</a> </div> ... </div>

JS

$("#output div").each(function() { console.log($(this).find("input").val()); $("a").each(function() { console.log($(this).text() + "\n"); }); });

Here is a fiddle.

最满意答案

您必须为anchor选择器设置context ,

$("a", this).each(function() {

DEMO

上面的代码将选择上下文中存在a元素。 在我们的例子中, this将指向我们正在迭代的div 。 如果你使用没有任何上下文的$("a")那么它将选择整个DOM中不存在于特定元素内的所有元素。

You have to set the context for for the anchor selector,

$("a", this).each(function() {

DEMO

The above code will select the a elements present in the context this. Here in our case this will point to the div that we are iterating. If you use $("a") without any context then it will select all the a elements present in the entire DOM not inside a particular element.