使用 CSS 导入 JavaScript 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8630625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:03:03  来源:igfitidea点击:

Import JavaScript files with CSS

javascriptcssimport

提问by henryaaron

Is it possible to use a CSS import file to import JavaScript pages?

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

@import url(Script.js);

回答by ?ime Vidas

The spec states the URL has to point to a style sheet:

规范规定 URL 必须指向样式表:

The '@import' rule allows users to import style rules from other style sheets. Any @import rules must follow all @charset rules and precede all other at-rules and rule sets in a style sheet. The '@import' keyword must be followed by the URI of the style sheet to include. A string is also allowed; it will be interpreted as if it had url(...) around it.

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

CSS3 Syntax Specification:http://www.w3.org/TR/css3-syntax/#import

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

回答by Frédéric Hamidi

No, this syntax is not supported.

不,不支持此语法。

You can, however, use the srcattribute of the <script>element to specify the location of an external script resource:

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

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

回答by Daff

No, you can only import CSS stylesheets or .less and .css stylesheets when using Less. You can do it the other way around however and import CSS stylsheets with JavaScript.

不,您只能在使用Less时导入 CSS 样式表或 .less 和 .css 样式表。但是,您可以反过来做,并使用 JavaScript导入CSS 样式表

回答by Cak Sup

// 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

How about this?

这个怎么样?