Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.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 使用JS在html中创建具有动态行和列的表_Javascript_Html_Css - Fatal编程技术网

Javascript 使用JS在html中创建具有动态行和列的表

Javascript 使用JS在html中创建具有动态行和列的表,javascript,html,css,Javascript,Html,Css,在我的例子中,脚本在显示html页面之前终止。因此,它无法访问HTMLDOM,并且在显示对话框时无法更新元素。这是一项要求 但是我需要根据不同的数据创建一个具有动态行和列的表 <html> <body style="overflow:hidden;"> <?js // here the js code must be. // I have a json object array, I need to crea

在我的例子中,脚本在显示html页面之前终止。因此,它无法访问HTMLDOM,并且在显示对话框时无法更新元素。这是一项要求

但是我需要根据不同的数据创建一个具有动态行和列的表

<html>
    <body style="overflow:hidden;">

        <?js
        // here the js code must be.
        // I have a json object array, I need to create a table to 
        //display these data. According to the size the table rows and 
         //columns must be changed. 
        ?>

        <!--Body div has autogenerated id.Please do not delete it. --> 
        <div id="bodydiv" class="Blank" style="background-color:rgb(247, 247, 247); height: 100%; width: 100%;">

        <!-- here the html page elements must be created -->
        </div>  

    </body>
</html>

这是一个非常常见的问题,因为:

  • javascript附加在html或
  • javascript在页面加载之前作为加载时间异步加载
解决此问题的一种方法是在加载dom后运行javascript:

// your table does not exist here
console.log(document.querySelectorAll('#bodydiv table').length);
window.onload = function whenLoaded() {
   // your table exist here
   console.log(document.querySelectorAll('#bodydiv table').length);
}
在大多数情况下,即使您的脚本是从资源下载的,这也可以正常工作


如果您愿意,只需在
标记的末尾添加
,它也会起作用。

这是一个非常常见的问题,因为:

var jsonArray;    //let's this is your json object array

var table = document.createElement("table");
for(var i=0;i<jsonArray.length;i++){

    var row = table.insertRow(i);

    //insert cells to the row based on the number of element you want to show in table from jsonArray
    var cell0 = row.insertCell(0);
    var cell1 = row.insertCell(1);

    //Assume key1 and key2 are keys in each jsonArray object
    cell1.innerHTML = jsonArray[i].key1; 
    cell2.innerHTML = jsonArray[i].key2;      
}

document.getElementById("divId").appendChild(table);    //consider a div with id 'div1' where you want to show the table
  • javascript附加在html或
  • javascript在页面加载之前作为加载时间异步加载
解决此问题的一种方法是在加载dom后运行javascript:

// your table does not exist here
console.log(document.querySelectorAll('#bodydiv table').length);
window.onload = function whenLoaded() {
   // your table exist here
   console.log(document.querySelectorAll('#bodydiv table').length);
}
在大多数情况下,即使您的脚本是从资源下载的,这也可以正常工作

如果愿意,只需在
标记的末尾添加
,它也应该可以工作。

var jsonArray//让我们看看这是您的json对象数组
var jsonArray;    //let's this is your json object array

var table = document.createElement("table");
for(var i=0;i<jsonArray.length;i++){

    var row = table.insertRow(i);

    //insert cells to the row based on the number of element you want to show in table from jsonArray
    var cell0 = row.insertCell(0);
    var cell1 = row.insertCell(1);

    //Assume key1 and key2 are keys in each jsonArray object
    cell1.innerHTML = jsonArray[i].key1; 
    cell2.innerHTML = jsonArray[i].key2;      
}

document.getElementById("divId").appendChild(table);    //consider a div with id 'div1' where you want to show the table
var table=document.createElement(“表”); 对于(var i=0;i
var jsonArray;//这是您的json对象数组
var table=document.createElement(“表”);

对于(var i=0;在body标记旁边,但在其他内容之前。在发布的代码示例中,我在body标记旁边的comments中提到了它们,但在其他内容之前。在发布的代码示例中,我在comments中提到了它们。非常感谢。我已经尝试过了。但是没有任何结果。非常感谢。我已经尝试过了。但是它确实做到了没有给出任何结果。我不知道这是如何添加到DOM的。我把它放在我的代码中,做了一个“查看源代码”——它不在那里。它唯一显示的地方是在屏幕上和Firefox检查器中。我不知道这是如何添加到DOM的。我把它放在我的代码中,做了一个“查看源代码”——它不在那里。它唯一显示的地方是在屏幕上,我n Firefox检查器。