Javascript 避免文档。写?还有别的选择吗?
我目前正在使用document.write将html注入到我的框架中。我正在使用主干、木偶、require.js和jQuery 我希望避免使用document.write,因为它会影响性能和浏览器 请提出一个替代方案 我当前的代码:Javascript 避免文档。写?还有别的选择吗?,javascript,jquery,backbone.js,marionette,Javascript,Jquery,Backbone.js,Marionette,我目前正在使用document.write将html注入到我的框架中。我正在使用主干、木偶、require.js和jQuery 我希望避免使用document.write,因为它会影响性能和浏览器 请提出一个替代方案 我当前的代码: var html = this.compilePageHtml(); //We get some HTML code this._previewWindow = window.open("about:blank", "PreviewWindow","
var html = this.compilePageHtml(); //We get some HTML code
this._previewWindow = window.open("about:blank", "PreviewWindow","width=320, height=570, scrollbars=yes" );
this._previewWindow.document.write(html); //Loads HTML in new window popup
您只需在新窗口的主体上使用
.innerHTML
属性即可
this._previewWindow = window.open("about:blank", "PreviewWindow","width=320, height=570, scrollbars=yes" );
this._previewWindow.document.body.innerHTML = html;
演示:为什么不改用
.html()
呢?。jQuery中的可能重复项:$(this.\u previewWindow.document).html(html);这将替换body元素中的html,而它应该是document。@MaxZoom-这取决于OP的html是什么(他们没有透露)。一个新的窗口/文档被赋予了一个骨架体,因此该代码将设置主体HTML。是的,你也可以设置document.documentElement
HTML。我添加了这个代码>它是即时加载的(而不是大约4-5秒的延迟),但我在jquery.mobile上看到了错误加载页面。这是div错误加载页面
似乎一切都正常,但我不明白为什么会出现这种错误。