简体   繁体   English

用简单的html dom解析Javascript生成的表

[英]Parsing Javascript generated table with simple html dom

I have a question. 我有个问题。 It is possible to parse Javascript generated html tables? 可以解析Javascript生成的html表吗? Because I tried with simple html dom, and I got a blank page for result every time. 因为我尝试使用简单的html dom,所以每次都会得到空白页面。

Here is the parsed website: 这是解析的网站:

<html>
<head>

<script src="informations.php" type="text/javascript" language="javascript"></script>


</head>
<body>

<script language="javascript" type="text/javascript">
<!--

// Draw table from 'jsData' array of objects
function drawTable(tbody) {
    var tr, td;
    tbody = document.getElementById(tbody);
    // loop through data source
    for (var i = 0; i < data.length; i++) {
        tr = tbody.insertRow(tbody.rows.length);
        tr.setAttribute("class", "f1");
        td = tr.insertCell(tr.cells.length);
        td.innerHTML = data[i][1];
        td = tr.insertCell(tr.cells.length);
        td.innerHTML = data[i][2];
td = tr.insertCell(tr.cells.length);
        td.setAttribute("align", "center");
        td.innerHTML = data[i][3];
        td = tr.insertCell(tr.cells.length);
        td.setAttribute("align", "center");
        td.innerHTML = data[i][4];
td = tr.insertCell(tr.cells.length);
        td.setAttribute("align", "center");
        td.innerHTML = data[i][5];

    }
}

//-->
</script>


<table>
<tr>
<td><h2>Informations</h2></td>
</tr>
</table>
<table class="sortable">
<thead>
<tr>
    <th>Name</th>
    <th>Information</th>
<th>Birthday</th>
<th>Postcode</th>
<th>Job</th>
</tr>
</thead>
<tbody id="FData"></tbody>
</table>

<script language="javascript" type="text/javascript">
drawTable("FData");
</script>
</body>
</html>

I have no problem with classic tables, but with js generated tables I got blank page for result everytime. 我对经典表没有问题,但是使用js生成的表,每次都会得到空白页面以获取结果。 What is the solution? 解决办法是什么?

try this code in your JS function. 在您的JS函数中尝试此代码。

// Find a element with id="myTable": //查找id为“ myTable”的元素:

var table = document.getElementById("myTable");

// Create an empty element and add it to the 1st position of the table: //创建一个空元素并将其添加到表格的第一位置:

var row = table.insertRow(0);

// Insert new cells ( elements) at the 1st and 2nd position of the "new" element: //在“ new”元素的第一和第二位置插入新的单元格(元素):

var cell1 = row.insertCell(0); var cell2 = row.insertCell(1);

// Add some text to the new cells: //在新单元格中添加一些文本:

cell1.innerHTML = "NEW CELL1"; cell2.innerHTML = "NEW CELL2";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM