简体   繁体   中英

How to separate $('ul li').text by comma

HTML

<ul>
  <li>List 1</li>
  <li>List 2</li>
<ul>

jQuery

$('ul li').text();

Current Output

List1List2

Expected Output

List1,List2

Question

How to separate them by comma?

Try to use .map() along with .get() to get that texts into an array and .join() it afterwards,

var values = $('#tags li').map(function(){ 
  return $(this).text(); 
}).get().join(','); // List1,List2

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