没有效果哦 是我写错了吗 代码如下: <!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"> <title> New Document </title> <head> <script language="javascript"> function cssOnload() { document.write("link=document.createElement('link')"); document.write("link.href='\1.css\'"); document.write("link.type='\text/css\'"); } </script> </head><body onload="cssOnload" >1111</body> </html>//1.css样式 body{ color:#ff0000} 实际的页面的字的颜色并没有变化
ok 我找到写出来了 var head = document.getElementsByTagName('HEAD').item(0); var style = document.createElement('link'); style.href = '1.css'; style.rel = 'stylesheet'; style.type = 'text/css'; head.appendChild(style);
<link id="cssid" type="text/css" />
是我写错了吗
代码如下:
<!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">
<title> New Document </title>
<head>
<script language="javascript">
function cssOnload()
{
document.write("link=document.createElement('link')");
document.write("link.href='\1.css\'");
document.write("link.type='\text/css\'");
}
</script>
</head><body onload="cssOnload" >1111</body>
</html>//1.css样式
body{ color:#ff0000}
实际的页面的字的颜色并没有变化
我找到写出来了
var head = document.getElementsByTagName('HEAD').item(0);
var style = document.createElement('link');
style.href = '1.css';
style.rel = 'stylesheet';
style.type = 'text/css';
head.appendChild(style);