Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将项目列表到javascript数组中_Javascript_Jquery_Arrays_Variables - Fatal编程技术网

将项目列表到javascript数组中

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

试图找出如何从“var字体”中获得一个合适的数组。目的是在输入值中有一个随机文本。目前,它将一起输出一长行文本(text1text2text3text4等)


代码中的问题是,您将所有“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 */

});