javascript文档.write脚本
javascript document.write script
编辑的定量
我正在尝试创建两个独立的HTML文档:main.HTML和sufler.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_。
相关文章:
- javascript文档.write脚本
- document.write(“anything”) 不会在 Internet Explorer 中作为外部脚本执行
- 如何确定 document.write() 是否会覆盖页面,即当前脚本是否异步加载
- 脚本下载和执行的顺序与 document.write .
- document.write 来自外部脚本,而不是在使用 javascript / jQuery 创建的 iframe
- 解析包含脚本标记和 document.write 的 HTML 字符串
- 使用getElementsByTagName运行包含document.write的脚本
- 使用document.write是跨浏览器跨域同步加载脚本的唯一可能方式
- 为什么这个脚本标记在用于document.write()之前被拆分了
- 如何在document.write中包含脚本
- 如何加载脚本,使其文档.Write进入隐藏元素进行操作
- 警告:从异步加载的外部脚本调用document.write()被忽略.这是如何解决的
- 结束脚本标记和文档.write在Visual Studio中被标记
- Javascript:运行";document.write";在createElement脚本中失败
- 如何使用response.write从javascript调用vb脚本
- document.write存在跨站点脚本问题
- 为什么document.write of script标记有时间滞后?以及脚本标记执行延迟的原因
- 为什么插入 document.write 的脚本可以更改变量
- 如何在运行时添加脚本?(文档.Write不是一个函数)
- “文档.Write '适合查找与当前运行的脚本相关联的元素