可以'我不明白为什么;t将行和单元格添加到表中
can't figure out why this doesn't add row and cell to table
当我运行脚本时,控制台中没有任何错误,只是不会添加行和单元格。当找到多行结果时,我需要添加一行和单元格。我有错误吗????这是的一小部分代码
var txt = "";
var i = 0;
for (state in stateMiles) {
i++;
var x =(stateMiles[state]);
(stateMiles[state])= parseFloat(x);
function appendRow() {
var tbl = document.getElementById("table"), // table reference
row = tbl.insertRow(tbl.rows.length), // append table row
i;
// insert table cells to the new row
for (i = 0; i < tbl.rows[0].cells.length; i++) {
createCell(row.insertCell(i), i, 'row');
}
}
$("#results").append
$(".state" + i).append(state);
$(".mile" + i).append(stateMiles[state]);
}
您的代码片段一团糟。您是否在for循环中声明了一个函数?
不要那样做。
这是像Angular、React等MVC风格库都非常擅长的事情,但如果你想做到这一点,这里有一些代码应该会有所帮助。
<script type="text/javascript">
$(function() {
var stateMiles = [
{
state: "CO",
miles: 5082
},
{
state: "TX",
miles: 582085
},
{
state: "IL",
miles: 9852
}
];
var tbody = $( "#my_table tbody" );
for( state_idx in stateMiles ) {
var state = stateMiles[ state_idx ];
tbody.append( $( "#row_template" ).html().replace( "{{state}}", state.state ).replace( "{{miles}}", state.miles ) );
}
});
</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<html>
<body>
<table id="my_table">
<thead>
<th>State</th>
<th>Miles</th>
</thead>
<tbody>
</tbody>
</table>
<script type="text/html" id="row_template">
<tr>
<td>{{state}}</td>
<td>{{miles}}</td>
</tr>
</script>
</body>
</html
相关文章:
- 可以'我不明白为什么;t将行和单元格添加到表中
- 如何在Angular中的表的所有单元格中添加链接
- jQuery-通过AJAX调用动态添加具有值的表单元格
- dhtmlx添加新行并启动单元格编辑器
- ExtJ将工具提示添加到网格单元格文本中
- 用于在表中添加行的Javascript代码将单元格中的数据复制到新行中
- 将工作表复制到新的电子表格,将单元格值添加到新电子表格名称的末尾
- 如何在 D3.grid 示例中向单元格添加文本 D3-js-building-a-grid-of-矩形
- 使用基于单选按钮选择的 jquery 将表格单元格添加到行尾
- 向动态创建的表中的单元格添加单击函数 - JavaScript
- 通过其 id 为表中的特定单元格添加颜色
- javascript如何每隔四个单元格添加一行
- 使用jquery向行单元格添加微调器
- 向数据表单元格添加侦听器
- 如何将单元格添加到表中,然后在N列之后换行
- 如何将表格单元格添加到表格行
- 在javascript中为单元格添加值
- 向由子元素事件触发的表单元格添加类/从中删除类
- javascript php为多个单元格添加时间戳
- 如何使用office-js以编程方式向工作表中的单元格添加超链接