使用CSS导入JavaScript文件

时间:2011-12-25 17:22:12

标签: javascript css import

是否可以使用CSS导入文件导入JavaScript页面?

@import url(Script.js);

4 个答案:

答案 0 :(得分:9)

规范声明URL必须指向样式表:

  

'@import'规则允许用户从其他样式导入样式规则   床单。任何@import规则都必须遵循所有@charset规则并且先于   样式表中的所有其他规则和规则集。 '@import'   关键字必须后跟要包含的样式表的URI 。一个   字符串也是允许的;它将被解释为它有url(...)   在它周围。

CSS3语法规范: http://www.w3.org/TR/css3-syntax/#import

答案 1 :(得分:5)

不,不支持此语法。

但是,您可以使用<script>元素的src属性来指定外部脚本资源的位置:

<script type="text/javascript" src="Script.js">
</script>

答案 2 :(得分:3)

不,您在使用Less时只能导入CSS样式表或.less和.css样式表。你可以反过来做,然后导入CSS stylsheets with JavaScript

答案 3 :(得分:0)

// Storing HTML code block in a variable
var codeBlock = '<div class="content">' +
                    '<h1>This is a heading</h1>' +
                    '<p>This is a paragraph of text.</p>' +
                    '<p><strong>Note:</strong> If you don\'t escape "quotes" properly, it will not work.</p>' +
                '</div>'; 
// Inserting the code block to wrapper element
document.getElementById("wrapper").innerHTML = codeBlock

这个怎么样?