[英]how to get value of inner html objects of a div
i have 3 span elements inside a div. 我在一个div内有3个span元素。 the whole div will be created dynamically inside a loop.
整个div将在循环内动态创建。 here is the code
这是代码
$.each(data.Payload, function(index, value){
stmt+='<li class="list-group-item">'+
'<div class="col-xs-12 col-sm-9">'+
'<div class="refugeeInfo">'+
'<span class="name" style="cursor: pointer">'+ value.firstName +' '+value.lastName+'</span><br/>'+
'<label><strong>unID: </strong><span class="unid" style="cursor: pointer;"> '+ value.unID +'</span></label>'+
'         '+
'<label><strong>rmsID: </strong><span class="rmsid" style="cursor: pointer">'+ value.rmsID +'</span></label><br/>'+
'</div>'+
'</li>'
});
now when i click on (".refugeeInfo") div i need to show the value of name, unID and rmsID. 现在,当我单击(“ .refugeeInfo”)div时,我需要显示名称,unID和rmsID的值。 wjat I tried
我尝试过
$('.refugeeInfo').click(function(){
var name=$(this).children('.name').text();
var unID=$(this).children('.unid').text();
var rmsID=$(this).children('.rmsid').text();
console.log("name: "+name);
console.log("unID: "+unID);
console.log("rmsID: "+rmsID);
})
but the result showing only name. 但结果仅显示名称。 unID and rmsID is vacant.
unID和rmsID空着。
Use find()
instead of children()
Problem is your .unid
and .rmsid
are nested inside a label
tag.. the selector children()
works only for direct descendants of the parent. 使用
find()
代替children()
问题是您的.unid
和.rmsid
嵌套在label
标记中。选择器children()
仅适用于父级的直接后代。 Here these 2 elements are not direct descendants. 这里这两个元素不是直接后代。
From the document . 从文件 。
The .children() method differs from .find() in that .children() only travels a single level down the DOM tree while .find() can traverse down multiple levels to select descendant elements (grandchildren, etc.) as well.
.children()方法与.find()的不同之处在于,.children()仅沿DOM树向下移动一个级别,而.find()可以向下遍历多个级别以选择后代元素(孙子等)。
$('.refugeeInfo').click(function(){
var name = $(this).children('.name').text();
var unID = $(this).find('.unid').text();
var rmsID = $(this).find('.rmsid').text();
console.log("name: " + name);
console.log("unID: " + unID);
console.log("rmsID: " + rmsID);
});
use find()
使用
find()
$('.refugeeInfo').click(function(){
var name=$(this).children('.name').text();
var unID=$(this).find('.unid').text();
var rmsID=$(this).find('.rmsid').text();
console.log("name: "+name);
console.log("unID: "+unID);
console.log("rmsID: "+rmsID);
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.