解决方法
在页面上加载外部CSS文件的常规方法是:
<head> <link rel="stylesheet" type="text/css" href="file.css" /> </head>
使用基本的JavaScript来做,你要创建一个Javascript函数:
<script type="text/javascript"> function loadCSS(filename){ var file = document.createElement("link"); file.setAttribute("rel","stylesheet"); file.setAttribute("type","text/css"); file.setAttribute("href",filename); document.head.appendChild(file); } //just call a function to load a new CSS: loadCSS("path_to_css/file.css"); </script>
您可以添加动态定义,如:
<script type="text/javascript"> var sheet = (function() { var style = document.createElement("style"); style.appendChild(document.createTextNode("")); document.head.appendChild(style); return style.sheet; })(); sheet.insertRule("span { visibility: hidden }",1); </script>