IE中的Element.appendChild()chokes [英] Element.appendChild() chokes in IE

查看:149
本文介绍了IE中的Element.appendChild()chokes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下javascript:

I have the following javascript:

  css = document.createElement('style');
  css.setAttribute('type', 'text/css');
  css_data = document.createTextNode('');
  css.appendChild(css_data);
  document.getElementsByTagName("head")[0].appendChild(css);

出于某种原因,仅在IE中,它在css.appendChild(css_data);上窒息
给出错误:对方法或属性访问的意外调用

for some reason, in IE only, it chokes on "css.appendChild(css_data);" Giving the error: "Unexpected call to method or property access"

发生了什么?

推荐答案

请改为:

var css = document.createElement('style');
css.setAttribute('type', 'text/css');

var cssText = '';
if(css.styleSheet) { // IE does it this way
    css.styleSheet.cssText = cssText
} else { // everyone else does it this way
    css.appendChild(document.createTextNode(cssText));
}

document.getElementsByTagName("head")[0].appendChild(css);

这篇关于IE中的Element.appendChild()chokes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆