admin管理员组文章数量:1323737
I am using below Html code and I want to get all the <li>
tags from the <div id="divSelect">
and I want to get text from all li tags.
Please help , how to use .each()
and .find()
using JQuery.
Thanks
I am using below Html code and I want to get all the <li>
tags from the <div id="divSelect">
and I want to get text from all li tags.
Please help , how to use .each()
and .find()
using JQuery.
Thanks
Share Improve this question edited Jul 21, 2014 at 9:18 Shaunak D 20.6k10 gold badges47 silver badges79 bronze badges asked Jul 21, 2014 at 9:05 prog1011prog1011 3,4954 gold badges33 silver badges59 bronze badges7 Answers
Reset to default 6Hey I have used your html and wrote a jQuery function which is using .each and .find to fetch all the li from the DIV.
we should use .find where we can use, its remened by jQuery.(its performance is good if it is user wisely)
html code:-
<div id="divSelect" class="custom dropdown">
<ul>
<li>text 1</li>
<li>text 2</li>
<li>text 3</li>
</ul>
</div>
and javascript code is:-
$("#divSelect").find("li").each(function()
{
var $li=$(this);
alert($li.text())
});
thanks
To get them in array,You can use:
var alllitexts=$('#divSelect ul li').map(function(){
return $(this).html();
}).get();
using each and getting them individually:
$("#divSelect ul li").each(function(){
alert($(this).html());
});
$("#divSelect > ul > li").text();
With this you get text from all li
elements.
fiddle
try this:
var liText='';
$('#divSelect ul li').each(function(){
liText+=$(this).html();
});
liText will contain all the "li"s texts.
This can be achieved by
$("#divSelect ul li").each(function(index){
alert ( index + ": " + $( this ).text() );
});
$("#divSelect").find("li").each(function(){
alert($(this).html());
});
Use map in jquery to collect all data ,this.outerHTML
in javascript to return the html element
var data=$("#divSelect ul li").map(function(){
return this.outerHTML;
}).get();
alert(data);
DEMO
本文标签: javascriptHow to find each li from Div and how to get text of each ltligt tagStack Overflow
版权声明:本文标题:javascript - How to find each li from Div and how to get text of each <li> tag? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742119593a2421633.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论