简体   繁体   中英

Select <a> values from ul li within a jQuery variable

I have a jQuery variable as the following

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.

Can any body help???

var arr = $($data).find('a[href="#"]').map(function(){
     return $(this).text()
}).get();

Demonstration (open the console)

You may use .map() :

var values = $("a[href='#']", $data).map(function() {
    return this.innerHTML;
}).get();

DEMO: http://jsfiddle.net/Vp7tN/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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