我一直在使用 document.write 生成页面的 HTML 部分,并使用循环填充它。例如,我需要在我的页面上创建以下 10 个内容:
<div class=" normal" id="1"
style="text-align:left;
top: 13px;
left: 5px;
height: 10em;
width: 12em;">
<div class = "wrap">
<div class = "show">
<strong>New York City
<p>Status: Cold</p>
</strong>
</div>
<div class = "noshow">
<P>0001: Normal</P>
</div>
<div class = "here">
<P>0001: online</P>
<P>0002: online</P>
</div>
</div>
</div>
我一直在做:
<script>
document.write("<div class=\"");
.. you get the idea</script>
使用 jquery 或 not-document.write 来执行此操作的另一种方法是什么?您还可以提供应用于我上面的代码的简短示例吗?
最佳答案
Jquery .html()可能有利于此目的。
<script>
$(function() {
var htmlString = "<div>.. you get the idea</div>";
$("body").html(htmlString);
});
</script>
或者这将附加 html: .append()
<script>
$(function() {
var htmlString = "<div>.. you get the idea</div>";
$("body").append($(htmlString));
});
</script>
关于javascript - Document.write 替代方案示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21629512/