简体   繁体   English

如何使用jQuery在div标签内找到特定的标签值

[英]How to find particular label value inside div tag using jQuery

I need to read the value of particular label inside the div tag using jQuery. 我需要使用jQuery读取div标签内特定标签的值。 Below is the HTML structure. 下面是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”的内部文字吗?

Try # id-selector 试试# id-selector

$('#redLabel').text();

or 要么

$('#redLabel').html();


$('#redLabel') -->refers to element with id redLabel $('#redLabel') - >引用id redLabel元素

$('#redLabel').text(); --> get text of element with id redLabel - >获取id redLabel的元素的文本

$('#redLabel').text('value'); --> set text of element with id redLabel - >设置id redLabel的元素的文本


.text() 。文本()

.html() html的()


ID must be unique. ID必须是唯一的。 Use classes for multiple items or refer to a group 使用多个项目的classes或引用一个组

Read Two HTML elements with same id attribute: How bad is it really? 读取具有相同id属性的两个HTML元素:它有多糟糕?

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() ? 另请参阅jQuery:text()和html()之间的区别是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM