javascript文档.write脚本

javascript document.write script

本文关键字:脚本 write 文档 javascript      更新时间:2023-09-26

编辑的定量

我正在尝试创建两个独立的HTML文档:main.HTMLsufler.HTML。我的想法是从main.HTML制sufler.HTML页面。为此,我找到了一个解决方案要在main.html页面上像字符串元素一样编写sufler的.html代码,请更改该字符串元素中的内容,并使用main.html中的document.write函数编写除了<script>功能外,工作正常。。。

main.html

<script type="text/javascript">
var HTMLstringPage1     = '<!DOCTYPE html><html><head><link href="stilius.css" rel="stylesheet" type="text/css" />',
    HTMLstringPage2     = '</body></html>',
    HTMLstringStyle     = ''x3Cscript type="text/javascript">function styluss(){document.getElementById("flip").style.font="normal normal 30px sans-serif";}'x3C/script>',
    HTMLstringDiv1      = '</head><body onload="styluss()"><div id="sufler"><div id="mov"><p id="flip">',
    HTMLstringDiv2      = '</p></div></div>';
var newWindow           = window.open('sufler.html','_blank','toolbar=no, scrollbars=no, resizable=no, height=615,width=815');
    newWindow.document.body.innerHTML = '';
    newWindow.document.write(HTMLstringPage1,HTMLstringDiv1+"Text"+HTMLstringDiv2,HTMLstringPage2); //works fine
//  newWindow.document.write(HTMLstringPage1,HTMLstringStyle,HTMLstringDiv1+"Text"+HTMLstringDiv2,HTMLstringPage2);//works except script function
</script>

有人能帮忙吗?

要动态添加脚本标记,最好这样做:

var newDoc = newWindow.document;
var script = newDoc.createElement("script");
script.type = "text/javascript";
var text = newDoc.createTextNode('function styluss(){document.getElementById("flip").style.font="normal normal 30px sans-serif";}');
script.appendChild(text);
newDoc.getElementsByTagName("head")[0].appendChild(script);

工作演示:http://jsfiddle.net/jfriend00/JqW5F/


但是,像这样动态地创建代码几乎是不需要的。由于根据定义,你通常已经知道你想让代码做什么,你可以有一个预先创建的函数,它接受一些参数,然后用正确的参数调用现有的函数来完成你想要的事情,而不是动态创建一个自定义函数:

function styleIt(id, fontStyle) {
    document.getElementById(id).style.font = fontStyle;
}
styleIt("flip", "normal normal 30px sans-serif");
styleIt("flip2", "normal normal 12px sans-serif");

您必须为<script>标签使用正常的左括号:

var HTMLstringStyle = '<script type="text/javascript">function styluss(){document.getElementById("flip").style.font="normal normal 30px sans-serif";}<'/script>';

虽然我不明白你为什么会用这个。。。将CCD_ 4标签附接到CCD_ 5或CCD_。