為什么不生效:html部分:<!DOCTYPE html><html lang="en"><head> ? ?<meta charset="UTF-8"> ? ?<title></title> ? ?<link href="CSSevent.css" rel="stylesheet" type="text/css"> ? ?<script> function changeColor(val){ ? ? ? ? ? ?//表示獲取第n+1(n>=0)個引入的外部CSS文件 var outterCSSfile=document.styleSheets[0].rules;// ? ? ? ? ? ?alert(outterCSSfile); ? ? ? ? ? ?//表示獲取該CSS文件的第n+1(n>=0)個選擇器 var styleOption=outterCSSfile[0]; if(val.value=="紅色"){ ? ? ? ? ? ? ? ?styleOption.style.backgroundColor="red"; } ? ? ? ? ? ?else if(val.value=="黑色"){ ? ? ? ? ? ? ? ?styleOption.style.backgroundColor="black"; } ? ? ? ?} ? ?</script></head><body><div class="colorBlock"></div><input type="button" value="紅色" onclick="changeColor(this)"><input type="button" value="黑色" onclick="changeColor(this)"></body></html>css部分:.colorBlock{ ? ? ?width: 200px; ? ? ?height:200px; ? ? ?background-color: black; ?}瀏覽器報錯:那應該怎么辦呢?
JS document.stylesheets[0].rules返回空
恩言
2016-02-03 13:52:32