jQuery 在子 div 中获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2312876/
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
jQuery get value within child div
提问by medium
I need to grab the text value of a child div.
我需要获取子 div 的文本值。
<div id='first'>
<div id='first_child'>A</div>
<div id='second_child'>B</div>
<div id='third_child'>C</div>
</div>
I am trying to grab the value B. I am currently trying this but it is not working,
我正在尝试获取值 B。我目前正在尝试此操作,但它不起作用,
var text_val = $('#first').next('#second_child').val();
回答by tvanfosson
You want to use children()
and text()
instead of val()
. Although, since what you are selecting has an id (and ids must be unique), you could also simply select based on the id without involving the container element at all.
您想使用children()
andtext()
而不是val()
. 虽然,由于您选择的内容有一个 id(并且 ids 必须是唯一的),您也可以简单地根据 id 进行选择,而根本不涉及容器元素。
The val()
method only works on input elements, textareas, and selects -- basically all form elements that contain data. To get the textual contents of a container, you need to use text()
(or html()
, if you want the mark up as well).
该val()
方法仅适用于输入元素、文本区域和选择——基本上所有包含数据的表单元素。要获取容器的文本内容,您需要使用text()
(或者html()
,如果您还想要标记)。
var text_val = $('#second_child').text(); //preferred
or
或者
var text_val = $('#first').children('#second_child').text(); // yours, corrected
回答by John Fisher
next() and nextAll() traverse the siblingsof the element, not the children. Instead, use "find()"
next() 和 nextAll() 遍历元素的兄弟元素,而不是子元素。相反,使用“find()”
var text_val = $('#first').find('#second_child').html();
You use val() when dealing with elements like inputs and textareas. html() will return whatever is under the div (which works in your example). text() will return the text in the element (wich would also work in your example).
在处理输入和文本区域等元素时,您可以使用 val()。html() 将返回 div 下的任何内容(在您的示例中有效)。text() 将返回元素中的文本(这也适用于您的示例)。
Your example also makes me suspicious of your design. If you have multiple ids set to "#second_child", your html is confused. Instead, use a class. However, if there's only one id "#second_child", then all you need is:
你的例子也让我怀疑你的设计。如果您将多个 id 设置为“#second_child”,您的 html 就会被混淆。相反,使用一个类。但是,如果只有一个 id "#second_child",那么您只需要:
var text_val = $('#second_child').text();
回答by widyakumara
you can simply:
你可以简单地:
var text_val = $('#second_child').text();
as pointed by previous answers. but, since you "need to grab the text value of a child div", i assume that the child div id probably not always available. you can try this instead:
正如之前的答案所指出的那样。但是,由于您“需要获取子 div 的文本值”,我认为子 div id 可能并不总是可用。你可以试试这个:
var text_val = $('#first').children().eq(1).text();
where 1 is the index of the child div (since it count starts from zero)
其中 1 是子 div 的索引(因为它从零开始计数)
回答by Teja Kantamneni
Try this...
尝试这个...
var text_val = $('#first > #second_child').text();
回答by Buhake Sindi
Simple,
简单的,
var text_val = $('#second_child').text();
Or your change,
或者你的改变,
var text_val = $('#first').children('#second_child').text();
This will return "B" from div of id "second_child"
这将从 ID 为“second_child”的 div 返回“B”
UPDATEChanged the second solution to children()
instead of nextAll()
.
更新将第二个解决方案改为children()
而不是nextAll()
.
回答by Kountay Dwivedi
For multiple elements with the same id:
对于具有相同 id 的多个元素:
var text_val = $('#first').children('#second_child').text();
For a single element with a particular id:
对于具有特定 id 的单个元素:
var text_val = $('#second_child').text();
回答by Red
If you need to go into lists, use this:
如果您需要进入列表,请使用以下命令:
jQuery('.first > ul > li:nth-child(6)').text();
jQuery('.first > ul > li:nth-child(6)').text();
Example code gets the value of the 6th item in a list.
示例代码获取列表中第 6 项的值。
回答by Sarfraz
........
…………
var text_val = $('#first').children('#second_child').text();