gpt4 book ai didi

javascript - jQuery 如何将我的有序列表转换为数组?

转载 作者:行者123 更新时间:2023-11-28 04:04:30 26 4
gpt4 key购买 nike

这是我的 list :

<ol>
<li>Audi</li>
<li>Kawasaki</li>
<li>Vauxhall</li>
</ol>

将列表项转换为数组的 jQuery 函数是什么?

var cars;
$('li').map(function() {
// converts the ordered list into a javascript array named 'cars'
});

谢谢!

最佳答案

如果您想要的是 li DOM 元素的数组,您可以使用 jQuery 的 toArray() :

var cars = $('li').toArray();

如果您需要的是一个包含 ['audi', 'kawasaki',...] 的数组,那么:

var cars = [];
$('li').each(function(i, elem) {
cars.push($(elem).text());
});

关于javascript - jQuery 如何将我的有序列表转换为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4655283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com