我在IE9上的代码中有一个错误。
<!doctype html>
<html>
<head>
<title>Create Node test</title>
</head>
<body>
<script type="text/javascript">
window.onload = function () {
var head = document.getElementsByTagName('head')[0];
var styleEl = document.createElement('style');
var css = "body {};";
if (styleEl.styleSheet){
styleEl.styleSheet.cssText = css;
} else {
styleEl.appendChild(document.createTextNode(css));
}
head.appendChild(styleEl);
}
</script>
</body>
</html>有人能告诉我我做错了什么吗?它在Chrome中运行得很好。
谢谢
要弄清楚:我想知道为什么这不起作用,我不想解决问题。我正在努力跟踪IE9中的一个错误,我认为这可能与createNode方法和样式元素有关。以前有没有人遇到过这个问题,或者是IE9中已知的错误?
发布于 2014-01-13 13:37:48
您可以始终使用这个:
<!doctype html>
<html>
<head>
<title>Create Node test</title>
<script type="text/javascript" language="javascript">
window.onload = function () {
var head = document.getElementsByTagName('head')[0];
var styleEl = document.createElement('style');
var css = "body {background-color:red;};";
styleEl.innerHTML = css;
head.appendChild(styleEl);
}
</script>
</head>
<body>
</body>
</html>https://stackoverflow.com/questions/21092490
复制相似问题