Dou*_*eri 6 javascript iframe doctype
我正在构建一个可嵌入其他站点的小部件.小部件是使用document.write()但我不知道如何使用javascript应用iframe文档类型创建的iframe.
这是我的代码:
document.write("<iframe scrolling=\"no\" frameborder=\"0\">");
document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
document.write("<html>");
document.write("<head></head><body></body>");
document.write("</html>");
document.write("</iframe>");
document.write("</div>");
Run Code Online (Sandbox Code Playgroud)
iframe已创建,但我没有应用doctype.有没有办法做到这一点?
谢谢
Chr*_*alo 13
为了写入iframe,你首先需要创建它,将它附加到文档,然后到达它的内部以获得它的句柄contentDocument.
以下是一些示例代码:
// create the iframe and attach it to the document
var iframe = document.createElement("iframe");
iframe.setAttribute("scrolling", "no");
iframe.setAttribute("frameborder", "0");
document.body.appendChild(iframe);
// find the iframe's document and write some content
var idocument = iframe.contentDocument;
idocument.open();
idocument.write("<!DOCTYPE html>");
idocument.write("<html>");
idocument.write("<head></head>");
idocument.write("<body>this is the iframe</body>");
idocument.write("</html>");
idocument.close();
// now have a look at your creation in the console
console.log(idocument);
Run Code Online (Sandbox Code Playgroud)
看到它在这个jsfiddle工作.
| 归档时间: |
|
| 查看次数: |
6790 次 |
| 最近记录: |