javascript 如何使用jQuery在div标签内找到特定的标签值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20146330/
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
How to find particular label value inside div tag using jQuery
提问by Dhaval Panchal
I need to read the value of particular label inside the div tag using jQuery. Below is the HTML structure.
我需要使用 jQuery 读取 div 标签内特定标签的值。下面是 HTML 结构。
<div id="myDiv">
<label id="greenLabel">Hello</label>
<label id="redLabel">There you go!</label>
</div>
Can anyone help me to find the inner text of "redLabel" ?
谁能帮我找到“redLabel”的内部文字?
回答by Tushar Gupta - curioustushar
Try #id-selector
$('#redLabel').text();
or
或者
$('#redLabel').html();
$('#redLabel')
$('#redLabel')
--> 引用元素 id
id
redLabel
redLabel
$('#redLabel').text();
--> get text of element with id
redLabel
$('#redLabel').text();
--> 获取元素的文本 id
redLabel
$('#redLabel').text('value');
--> set text of element with id
redLabel
$('#redLabel').text('value');
--> 设置元素文本 id
redLabel
.text()。文本()
ID
ID
必须是唯一的。使用classes
classes
多个项目或指一组Read Two HTML elements with same id attribute: How bad is it really?
回答by Rituraj ratan
try this
试试这个
$("#myDiv #redLabel").text();
//go to div then label
// or can try if in label there is no element
$("#myDiv #redLabel").html();
you can also use $("#redLabel")
at the place of $("#myDiv #redLabel")
您也可以$("#redLabel")
在$("#myDiv #redLabel")
see also What is the difference between jQuery: text() and html() ?