2011-10-14 6 views

답변

4

어쩌면 동적으로 CSS를 만들 수 있습니다.

var styleElement = document.createElement("style"); 
styleElement.type = "text/css"; 
if (styleElement.styleSheet) { 
    styleElement.styleSheet.cssText = "a { color: red }"; 
} else { 
    styleElement.appendChild(document.createTextNode("a { color: red; }")); 
} 
document.getElementsByTagName("head")[0].appendChild(styleElement); 
+0

1, 카우 벨하지 대답은 많은 감사합니다! 아래쪽 유권자에게 –

2

document.anchors는 배열을 반환하므로 루프를 통과해야합니다.

var anchors = document.anchors; 
for(var i=0, m=anchors.length; i<m; i++){ 
    anchors[i].style.color = "red"; 
} 
0

은 이미 답했지만 어쨌든 잘 내 버전 :

<html> 
    <head> 
     <script type="text/javascript"> 
     var ChangeColors = function() { 
      var elem = document.createElement('style'); 
      elem.setAttribute("type", "text/css"); 
      var style = document.createTextNode("A, A:hover, A:visited { color: red; }") 
      elem.appendChild(style); 
      document.getElementsByTagName("head")[0].appendChild(elem); 
     } 
     </script> 
    </head> 
    <body> 
     <a href="#">Some Link</a><br /> 
     <a href="http://www.google.com">Some Other Link</a><br /> 
     <input type="button" onclick="ChangeColors();"/> 
     <a href="#">Some Link 2</a><br /> 
     <a href="#">Some Link 3</a><br /> 
    </body> 
</html> 
+0

: 왜이 ​​게시물에 투표했는지 설명 할 수 있다면 매우 감사하겠습니다. 고맙습니다. – Vinzenz