[英]Select <a> values from ul li within a jQuery variable
I have a jQuery variable as the following 我有一个jQuery变量,如下所示
var $data = "<li id='root'><a href='#'>Root node</a><ul><li><a href='#'>Child node</a></li></ul></li><li id='root2'><a href='#'>Root 2 node</a><ul><li><a href='#'>Child 2 Parent Node</a><ul><li><a href='#'>Child 2 child1</a></li></ul><ul><li><a href='#'>Child 2 child2</a></li></ul></li></ul></li>";
I want to fetch the values of each <a href=#>
values and push it into an array. 我想获取每个<a href=#>
值的值并将其推入数组。
Can any body help??? 任何身体都可以帮忙吗???
var arr = $($data).find('a[href="#"]').map(function(){
return $(this).text()
}).get();
Demonstration (open the console) 演示 (打开控制台)
You may use .map()
: 您可以使用.map()
:
var values = $("a[href='#']", $data).map(function() {
return this.innerHTML;
}).get();
DEMO: http://jsfiddle.net/Vp7tN/ 演示: http : //jsfiddle.net/Vp7tN/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.