【发布时间】:2011-02-15 09:49:18
【问题描述】:
我在尝试编写跨浏览器脚本时遇到了一个奇怪的问题。基本上我的标题是这样的
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head>
然后在body标签中:
<p id="hey">Hey</p>
<input type="button" value="attachStyle" name="attachStyle" onclick="attachStyle();"></input>
<script>
function attachStyle() {
var strVar="";
strVar += "<style type='text\/css'>#hey {border:5px solid red;}<\/script>";
$("head").append(strVar);
}
</script>
该按钮在 Firefox 中有效,但在 Chrome 中无效。当我在开发者工具中查看 html DOM 元素时,样式标签被插入但没有内容,像这样:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<style type='text/css'></script>
</head>
我很好奇这是什么原因造成的?以及如何以跨浏览器的方式创建 CSS 样式?谢谢!
【问题讨论】:
标签: javascript css dom google-chrome