Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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
向HTML页面添加javaScript数组?_Javascript_Jquery_Html_Arrays - Fatal编程技术网

向HTML页面添加javaScript数组?

向HTML页面添加javaScript数组?,javascript,jquery,html,arrays,Javascript,Jquery,Html,Arrays,我正在尝试向网页添加数组。我尝试了下面展示的几种不同的代码,但都不起作用。我希望输出类似于以下列表: 文本1 文本2 文本3 到目前为止,我使用的代码是: var i; var test = new Array(); test[0] = "text1"; test[1] = "text2"; test[2] = "text3"; // first attempt $('#here').html(test.join(' ')); // second attempt $(document).r

我正在尝试向网页添加数组。我尝试了下面展示的几种不同的代码,但都不起作用。我希望输出类似于以下列表:

文本1

文本2

文本3

到目前为止,我使用的代码是:

var i;
var test = new Array();
test[0] = "text1";
test[1] = "text2";
test[2] = "text3";

// first attempt
$('#here').html(test.join(' '));

// second attempt
$(document).ready(function() {
    var testList="";
    for (i=0;i<test.length; i++) {
        testList+=  test[i]  + '<br />';
    }
    $('#here').html('testList');
    songList="";
}); 
vari;
var test=新数组();
测试[0]=“text1”;
测试[1]=“text2”;
测试[2]=“text3”;
//第一次尝试
$('#here').html(test.join('');
//第二次尝试
$(文档).ready(函数(){
var testList=“”;
对于(i=0;i尝试不使用引号:

$('#here').html(testList);
-或-

$('#here').html(test.join('
');

另一种方法:

var html = '';                                    // string
$.each(test,function(i,val){                      // loop through array
    var newDiv = $('<div/>').html(val);           // build a div around each value
    html += $('<div>').append(newDiv.clone()).remove().html();   
       // get the html by
       //   1. cloning the object
       //   2. wrapping it
       //   3. getting that html
       //   4. then deleting the wrap
       // courtesy of (http://jquery-howto.blogspot.com/2009/02/how-to-get-full-html-string-including.html)
});

$('#here').html(html);
var html='';//字符串
$.each(测试,函数(i,val){//通过数组循环
var newDiv=$('').html(val);//围绕每个值构建一个div
html+=$('').append(newDiv.clone()).remove().html();
//通过以下方式获取html
//1.克隆对象
//2.包装
//3.获取html
//4.然后删除换行符
//承蒙(http://jquery-howto.blogspot.com/2009/02/how-to-get-full-html-string-including.html)
});
$('#here').html(html);
后者中可能有更多的代码,但如果您想添加ID、类或其他属性,从长远来看,它会更干净。只需将其粘贴到函数中并修改jQuery即可。

尝试更改行

$('#here').html('testList') 

这一行:

$('#here').html('testList');

testList
周围不应该有单引号-您希望使用变量的内容,而不是字符串文字“testList”。

不要将变量作为字符串传递:
$('#here').html('testList');
不带引号传递:
$('#here').html(testList);
这是最简单的版本:

$(document).ready(function() {
    var test = ["text1", "text2", "text3"];
    $('#here').html(test.join("<br>"));
}); 
$(文档).ready(函数(){
var测试=[“text1”、“text2”、“text3”];
$('#here').html(test.join(“
”)); });
如果从
testList
中删除单引号,您所拥有的一切都会起作用。但是,如果您想要一个实际的无序列表,您可以这样做。()

var test=newarray();
测试[0]=“text1”;
测试[1]=“text2”;
测试[2]=“text3”;
//第一次尝试
$('#here').html(test.join('');
//第二次尝试
$(文档).ready(函数(){
var testList=$(“
    ”);
    对于(var i=0;我在JavaScript控制台中是否收到任何错误消息?是否包含jQuery库?与问题无关,但这是一种创建数组的糟糕方式。只需使用
    var test=[“text1”、“text2”、“text3”];
    @rorymcrossan那么这很奇怪,因为vol7ron的答案直到现在才出现在我面前(re)多次加载该问题。查看每个答案在X分钟前的答案,首先是vol7ron的增量。不过,我注意到SOs new update polling有一些奇怪之处。是的,对不起,我无意中删除了它-是为了编辑它…不必担心,我们知道如何读取时间。但是,直到完全刷新后,他的答案才出现在页面上完成了。它不像其他新答案那样冒泡。
    $('#here').html('testList');
    
    $(document).ready(function() {
        var test = ["text1", "text2", "text3"];
        $('#here').html(test.join("<br>"));
    }); 
    
    var test = new Array();
    test[0] = "text1";
    test[1] = "text2";
    test[2] = "text3";
    
    // first attempt
    $('#here').html(test.join(' '));
    
    // second attempt
    $(document).ready(function() {
        var testList=$("<ul></ul>");
        for (var i=0;i<test.length; i++) {
            $(testList).append($("<li></li>").text(test[i]));
        }
        $('#here').html(testList);
        songList="";
    }); ​