简体   繁体   English

如何在 IE 中复制内联样式元素?

[英]How do you copy an inline style element in IE?

IE does not allow writing to the innerHTML property of style or head elements. IE 不允许写入 style 或 head 元素的 innerHTML 属性。 So how do you copy a style element from the head of one document to another?那么如何将一个样式元素从一个文档的头部复制到另一个?

function copy_style(src_style_tag) {    
    var tmp_div = document.createElement('div');
    var innerHTML = src_style_tag.innerHTML;
    tmp_div.innerHTML = '<p>x</p><style type="text/css">' + innerHTML + '</style>';

    return tmp_div.getElementsByTagName('style')[0];
}

The magic is that you need the <p> tag in the innerHTML of the tmp_div .神奇的是,您需要在tmp_divinnerHTML中使用<p>标记。 Without it, IE does not accept the style element.没有它,IE 不接受样式元素。

如果您想复制一些元素,请尝试将Node.cloneNode(true)Node.appendChild一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM