2017-10-20 69 views

回答

5

只需使用.map.get - 有没有需要中间变量或循环

const liTexts = 
 
    $ ('#wa li') 
 
    .map ((idx,elem) => $(elem).text()) 
 
    .get() 
 
    
 
console.log (liTexts) 
 
// [ 'one', 'two', 'three' ]
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<ul id="wa"> 
 
    <li>one</li> 
 
    <li>two</li> 
 
    <li>three</li> 
 
</ul>

+2

男人,其他人是最糟糕的,我是对吗? (开玩笑,我同意'.map()'是去这里的方式。) – nnnnnn

+0

一秒钟我以为你有'地图'的回调不正确,在我检查jquery的文档之前,并意识到他们的API只是倒退。另外,很高兴认识你@nnnnnn – aaaaaa

+0

@aaaaaa,亚,jquery是新的php – naomik

2
var myArr = []; // define the array 
$('#wa li').each(function (i) { 
    myArr.push($(this).text()); // push the value into the array 
}); 
console.log(myArr); // ['hello', 'world', ...] use the array 
2

您可以使用array.push()的值到数组

var myArr = []; // define the array 
$('#wa li').each(function (i) { 
    myArr.push($(this).text()); 
}); 
相关问题