将项目列表到javascript数组中
试图找出如何从“var字体”中获得一个合适的数组。目的是在输入值中有一个随机文本。目前,它将一起输出一长行文本(text1text2text3text4等)将项目列表到javascript数组中,javascript,jquery,arrays,variables,Javascript,Jquery,Arrays,Variables,试图找出如何从“var字体”中获得一个合适的数组。目的是在输入值中有一个随机文本。目前,它将一起输出一长行文本(text1text2text3text4等) 代码中的问题是,您将所有“li”元素的文本全部输入到“txt”变量中。因此,“字体”是长度为1的数组 您需要在每个列表项上循环以正确构建“字体”数组。比如: $(document).ready(function(){ var fonts = []; $(".oppa").each(function(){ $(this
代码中的问题是,您将所有“li”元素的文本全部输入到“txt”变量中。因此,“字体”是长度为1的数组 您需要在每个列表项上循环以正确构建“字体”数组。比如:
$(document).ready(function(){
var fonts = [];
$(".oppa").each(function(){
$(this).find("li").each(function(){
fonts.push($(this).text());
});
});
alert(fonts.length); // fonts is now an array with length equal to your number of list items
/* Rest of your code goes here */
});
最好的
$(document).ready(function(){
var fonts = [];
$(".oppa").each(function(){
$(this).find("li").each(function(){
fonts.push($(this).text());
});
});
alert(fonts.length); // fonts is now an array with length equal to your number of list items
/* Rest of your code goes here */
});