87,776
社区成员




<script>
function includeStyleElement(styles,styleId) {
if (document.getElementById(styleId)) {
return
}
var style = document.createElement("style");
style.id = styleId;
//为ie设置属性
/*if (isIE()) {
style.type = "text/css";
style.media = "screen"
}*/
(document.getElementsByTagName("head")[0] || document.body).appendChild(style);
if (style.styleSheet) { //for ie
style.styleSheet.cssText = styles;
} else {//for w3c
style.appendChild(document.createTextNode(styles));
}
}
var styles = "#div{background-color: #FF3300; color:#FFFFFF }";
includeStyleElement(styles,"newstyle");
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
</style>
</head>
<body>
<script>
function includeStyleElement(styles,styleId)
{
if (document.getElementById(styleId))
{
return
}
var style = document.createElement("style");
style.id = styleId;
(document.getElementsByTagName("head")[0] || document.body).appendChild(style);
if(style.styleSheet)
{ //for ie
style.styleSheet.cssText = styles;
}
else
{//for w3c
style.appendChild(document.createTextNode(styles));
}
}
var styles = "#my{background-color: #FF3300; color:#FFFFFF }";
includeStyleElement(styles,"newstyle");
</script>
<div id="my" >
成功
</div>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
</style>
</head>
<body>
<script>
function includeStyleElement(styles,styleId)
{
if (document.getElementById(styleId))
{
return
}
var style = document.createElement("style");
style.id = styleId;
(document.getElementsByTagName("head")[0] || document.body).appendChild(style);
if(style.styleSheet)
{ //for ie
style.styleSheet.cssText = styles;
}
else
{//for w3c
style.appendChild(document.createTextNode(styles));
}
}
var styles = "#my{background-color: #FF3300; color:#FFFFFF }";
includeStyleElement(styles,"newstyle");
</script>
<div id="my" >
</div>
</body>
</html>
能不能帮忙看看这段代码为什么错了?[/quote]
代码没有错误。只是你的div中没有内容,效果看不到而已
var oHead = document.getElementsByTagName('HEAD').item(0);
var oScript= document.createElement("script");
oScript.type = "text/javascript";
oScript.src="test.js";
oHead.appendChild( oScript);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
</style>
</head>
<body>
<script>
function includeStyleElement(styles,styleId)
{
if (document.getElementById(styleId))
{
return
}
var style = document.createElement("style");
style.id = styleId;
(document.getElementsByTagName("head")[0] || document.body).appendChild(style);
if(style.styleSheet)
{ //for ie
style.styleSheet.cssText = styles;
}
else
{//for w3c
style.appendChild(document.createTextNode(styles));
}
}
var styles = "#my{background-color: #FF3300; color:#FFFFFF }";
includeStyleElement(styles,"newstyle");
</script>
<div id="my" >
</div>
</body>
</html>
能不能帮忙看看这段代码为什么错了?
<script>
function includeLinkStyle(url) {
var link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = url;
document.getElementsByTagName("head")[0].appendChild(link);
}
includeLinkStyle("/css/reset.cssv=20140222"
</script>