如何在javascript中将字符串打印为html
How to print a string to html in javascript?
我正在尝试通过 document.write()
在 javascript 中将字符串打印为 html,但似乎当我在函数中使用document.write()
其他代码时不再起作用。
JavaScript 代码:
<script type="text/javascript">
var my_d = {{ pass_kegg|safe }};
var myList_tmp = {{ pass_tmp|safe }};
var epgd = "EPGD"
var kegg = "KEGG"
function buildHtmlTable(myList,printSource) {
var columns = addAllColumnHeaders(myList);
document.write(printSource);
// Builds the HTML Table out of myList json data from Ivy restful service.
}
function addAllColumnHeaders(myList) {
// Adds a header row to the table and returns the set of columns.
// Need to do union of keys from all records as some records may not contain
// all records
}
</script>
网页代码:
<body onload="buildHtmlTable(my_d, epgd);buildHtmlTable(myList_tmp, kegg)">
<table id="excelDataTable" border="1">
</table>
</body>
最初,有一个从JSON转换的html表可以在网页中看到。但是当我使用document.write()
打印字符串时epgd
我的网页中只能看到两个字符串。似乎其他代码尚未运行。我在谷歌上搜索过这个问题,有人说document.write()
只能与页面onload
一起使用,但如果在运行时使用它,它会替换整个文档。那么除了document.write()
之外,还有人可以为我提供一种在 JavaScript 中打印字符串的方法吗?
第一次尝试:
document.getElementById("mytableid").innerHTML += "<tr><td>str</td></tr>";
但我建议如下:
var myTable = document.getElementById("myTableId");
var row = myTable.insertRow(-1);
var cell = roww.insertCell(-1);
cell.textContent="data1";
例如,如果您有
var data = [
[ "one" , "two" ],
[ "alpha", "betha" ],
[ "a", "b" ]
];
var myTable = document.getElementById("myTableId");
data.forEach(function(dataRows){
var row = myTable.insertRow(-1);
dataRows.forEach(function(dataCell){
var cell = row.insertCell(-1);
cell.textContent = dataCell;
});
});
**<b>This code is working fine. I am using this code</b>**
//define json in string
var jsonPublishData ='[{"tag":"div","attr":[{"class":"box-footer"}],"child":"Hello"}]';
//convert json to html
var resp_html = json_to_html(jsonPublishData, true);
//define a variable and store html respons data in this variable
var publishButtonHTML= resp_html.outerHTML
//alert out
alert(publishButtonHTML);
//print out on console
console.log(publishButtonHTML);
// Start finction for JSON to HTML
function json_to_html(json_components, isDiv = false, finalHTML = "")
{
var jsoncompo = (isDiv) ? JSON.parse(json_components) : json_components;
var json_len = (jsoncompo).length;
if (finalHTML == "" && isDiv)
{
finalHTML = document.createElement("div");
}
for (var i = 0; i < json_len; i++)
{
var childele = '';
var html_components = jsoncompo[i];
var attributes_arr = html_components.attr;
finalHTML1 = createstructure(html_components.tag, attributes_arr, html_components.child);
finalHTML.appendChild(finalHTML1);
}
return finalHTML;
}
// End finction for JSON to HTML
// Start finction for create HTMLstructure from json
function createstructure(type, props, children) {
var el = document.createElement(type),
key;
for (key in props) {
var keyprops = props[key];
var hasOwn = Object.keys(keyprops);
for (var i = 0; i < hasOwn.length; i++)
{
el.setAttribute(hasOwn[i], keyprops[hasOwn[i]]);
}
}
if (typeof children != 'undefined') {
if (Array.isArray(children)) {
json_to_html(children, false, el);
} else {
textnode = document.createTextNode(children);
el.appendChild(textnode);
}
}
return el;
}
// Start finction for create HTMLstructure from json
相关文章:
- 跳过x个字符后,如何打印特定字符串
- 打印从Spring MVC Controller发送的字符串数组只显示“;toJSON”;在javascript中
- 如何在javascript中将字符串打印为html
- 当单击按钮时,试图使字符串数组按1乘1的顺序打印出来
- 用Ruby/Javascript解码带引号的可打印字符串
- 打印字符串的行数
- 如何在javascript中打印字符串数组
- 如何从 xmlDoc 打印 xml 字符串
- 打印出字符串中所有单词的最后一个字符
- 将指数值打印为字符串而不展开
- 如何编写一个测试来验证函数是否已成功将字符串列表(在数组中指定)打印到屏幕上
- Javascript - 如何获取字符串,打印单词,字符,空格,平均单词长度的#并作为对象返回
- 将变量打印到控制台时出现意外字符串:[对象窗口]
- 带有两个参数的“data”回调不会将buffer.toString()打印为字符串
- 在 JavaScript alert() 中打印 EL 字符串 “RP” 会导致 JS 错误“'RP'
- 使用数组处理 JavaScript 字符串 为什么它在控制台上打印“R”而不是“Z”
- 如何使用子字符串打印分隔字符串
- 将 JavaScript 字符串打印为字符串文本
- JQuery 字符串打印输出消失
- 仅从文本框中的字符串打印元音