document.write

时间:2018-02-26 08:07:37

标签: javascript html

我有下面的简单代码,当我运行它时,

元素与中心对齐,但当我在窗口中打开它并使用document.write它没有定义的样式/对齐< / p>

<!DOCTYPE html>
<html>
<body>



<p align="Center">This is a p element<br>

This is also a p element.<br>

This is also a p element - Click the button to open a doc.</p>

<button onclick="myFunction()">Try it</button>

<script>
    function myFunction() {
        win = window.open();
        win.document.open();
        var x = document.getElementsByTagName("p");
        for (var i = 0; i < x.length; i++) {
            win.document.write(x[i].innerHTML);
        }
    }
</script>

</body>
</html>

enter image description here

enter image description here

任何想法如何在写入另一个文档时保持HTML的原始格式?

谢谢

1 个答案:

答案 0 :(得分:2)

  

任何想法如何在写作时保持HTML的原始格式   进入另一个文件?

您需要写outerHTML而不是innerHTML,因为align=center是构成标记外部的p的属性(innerHTML不会有它)

 win.document.write(x[i].outerHTML);
相关问题