有没有办法通过HTML文件中的变量包含css文件?

时间:2015-12-26 04:15:37

标签: html css html5 template-engine

我正在寻找一种通过html文件中的变量包含css文件的方法。

<html>
  <head>
    <!-- variable = "/css/style.css" -->
    <link rel="stylesheet" href=variable>
    <link rel="stylesheet" href=variable>
  </head>
  <body>
    <!-- Do anything -->
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

您可以使用JavaScript执行此操作...创建一个JavaScript变量,并根据其值更改href属性的值,如下所示

<link href="url.css" id="selector" type="text/css" />
<script> var variable = 'value`;  </script>
...
<script>
if (variable) {  // Or whatever you need to do
  document.getElementById("selector").href = "another.css";
}
</script>

答案 1 :(得分:0)

这也有效....

 <script type="text/javascript">
                    var target = document.createElement("script");
                    target.type = "text/javascript";
                    target.src = "script/knockout.js";
                    document.getElementById('head').appendChild(target);

                    var cssTar = document.createElement("link");
                    cssTar.rel = "stylesheet";
                    cssTar.type = "text/css";
                    cssTar.href = "Scripts/StyleSheet1.css";
                    document.getElementById('headid').appendChild(cssTar);
           </script>