Cant Create STYLE tag in iframe head

江枫思渺然 提交于 2019-12-13 06:14:03

问题


Here's jsfiddle --> http://jsfiddle.net/uTy5j/7/embedded/result/

I'm using CodeMirror and it seems that Codemirror is erasing my style tag that I create with:

var preview = document.getElementById('preview');
var mypreview = preview.contentDocument;
mypreview.open();
mypreview.close();

var style = document.createElement('style');
style.id = 'mycssid';
style.type ='text/css';
mypreview.head.appendChild(style);

Can anyone figure out how I can get this style tag appended to the head of the iframe after everything is loaded?

The jsFiddle has all codemirror scripts and styles loaded in the external resources.

Here's jsfiddle --> http://jsfiddle.net/uTy5j/7/embedded/result/


回答1:


You just have to wait until the iframe is loaded before you try to insert stuff:

preview.onload = function() {
    var style = document.createElement('style');
        style.id = 'mycssid';
        style.type ='text/css';
        mypreview.head.appendChild(style);
}

FIDDLE



来源:https://stackoverflow.com/questions/17709234/cant-create-style-tag-in-iframe-head

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!