javascript-HTML表格标题无法正确显示
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-HTML表格标题无法正确显示,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2573字,纯文字阅读大概需要4分钟。
内容图文
![javascript-HTML表格标题无法正确显示](/upload/InfoBanner/zyjiaocheng/676/31adf60a48fb4d89b25b746d3a1041b3.jpg)
我有一个生成的表,看起来很好,除了表标题未与数据对齐,任何帮助将不胜感激.
该代码获取JSON,然后在构建表时循环遍历它,以最终将其显示在HTML页面上的div中.
代码如下:
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
* {
margin: 0;
}
html,
body {
background-color: white;
font-family: arial, helvetica, verdana, sans-serif;
font-size: 16pt;
}
.d0 {
background-color: #EFFBFB; /*#FCF6CF;*/
}
.d1 {
background-color: #E0F8F7; /*#FEFEF2;*/
}
</style>
</head>
<body>
<div id="des"></div>
</body>
</html>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
(function($) {
var json = {
1: { description:"Description", code:"1234", code1:"1234", code2:"1224"},
2: { description:"Description", code:"1234", code1:"1234", code2:"1224"},
3: { description:"Description", code:"1234", code1:"1234", code2:"1224"},
4: { description:"Description", code:"1234", code1:"1234", code2:"1224"},
5: { description:"Description", code:"1234", code1:"1234", code2:"1224"},
6: { description:"Description", code:"1234", code1:"1234", code2:"1224"},
7: { description:"Description", code:"1234", code1:"1234", code2:"1224"},
8: { description:"Description", code:"1234", code1:"1234", code2:"1224"},
9: { description:"Description", code:"1234", code1:"1234", code2:"1224"},
10: { description:"Description", code:"1234", code1:"1234",
code2:"1224"}
};
var tableHeader =
"<table>" +
"<tr>" +
"<th>Actions</th>" +
"<th>Description</th>" +
"<th>Code</th>" +
"<th>Code1</th>" +
"<th>Code2</th>" +
"</tr>";
var tableFooter = "</table>";
console.log(tableHeader);
$("#des").append(tableHeader);
$.each(json, function(key, value) {
var cssName = "d";
var cssKey = key % 2 == 0 ? 1 : 0;
cssKey = cssName + cssKey;
var html =
"<tr class=\"" + cssKey + "\">" +
"<td><a href=\"/common/Service?id=" + key + "\">Modify</a></td>" +
"<td>" + this.description + "</td>" +
"<td>" + this.code + "</td>" +
"<td>" + this.code1 + "</td>" +
"<td>" + this.code2 + "</td>" +
"</tr>";
console.log(html);
$("#des").append(html);
});
$("#des").append(tableFooter);
console.log(tableFooter);
})(jQuery);
解决方法:
jQuery将自动关闭您创建的任何HTML元素.您不能附加< table>标签与< / table>分开标签.
所以代替
$("#des").append(tableHeader);
// ....
$("#des").append(html);
// ....
$("#des").append(tableFooter);
您应该先附加空表,然后再将行附加到表主体:
$("#des").append(tableHeader + tableFooter);
// ...
$("#des tbody").append(html);
更好的是,将所有内容组合成一个字符串,然后一次全部附加以提高性能:
var table_html = tableHeader;
// ....
table_html += html;
// ....
table_html += tableFooter;
$("#des").append(table_html);
内容总结
以上是互联网集市为您收集整理的javascript-HTML表格标题无法正确显示全部内容,希望文章能够帮你解决javascript-HTML表格标题无法正确显示所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。