javascript - Document.write 替代方案示例

标签 javascript jquery html

我一直在使用 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/

相关文章:

javascript - 安卓:用JS设置window.location

javascript - JavaScript 中的 Web 服务调用和 JSON 数组操作

javascript - jQuery 创建的列表需要很多时间

jquery - 如何获得溢出:auto property set in css的div的高度

javascript - 页面加载后 Bootstrap 总是改变元素在手机上的绝对位置

javascript - 在 contentEditable 元素上按 Enter 键时插入 BR 或 P 标签的跨浏览器方式

javascript - 如何防止全日历中外部事件的突出显示(灰色背景)

jquery - 如何在 Nightwatch 中使用 jquery

jquery - 页脚上方的 float 社交图标

javascript - 如何使固定元素在滚动时隐藏,然后在用户不滚动时出现