简体   繁体   English

在同一文件夹中加载多个样式表/脚本的快捷方式

[英]Shortcut to load multiple stylesheets/scripts in the same folder

I have been working on a web page and would like to load multiple stylesheets in an external library. 我一直在网页上工作,想在外部库中加载多个样式表。

Unfortunately, this library has many CSS files spread under the same folder. 不幸的是,该库在同一文件夹下散布了许多CSS文件。

The names are complicated and its such a pain to manually link it one by one. 名称很复杂,手动将其链接起来很麻烦。

As it 因为它

<link type="text/css" href="site/libraries/folder/highlight-areas.css"></link>
...

Is there a shortcut that loads all CSS files on the same page within the folder site/libraries/folder 是否有快捷方式将所有CSS文件加载到文件夹site / libraries / folder的同一页面上

I know how to do this with Ruby on Rails but that is another domain. 我知道如何使用Ruby on Rails做到这一点,但这是另一个领域。

Is there a similar technique available on the client side? 客户端有类似的技术吗?

Thanks in advance 提前致谢

Would the CSS @import function help you? CSS @import函数对您有帮助吗? It allows you to import a CSS file into another, so you could create one master CSS that links to the rest of library styles. 它允许您将一个CSS文件导入另一个文件,因此您可以创建一个链接到其余库样式的主CSS。

Not as such, no. 不是这样,不。 Javascript does not have access to server-side information at all. Javascript根本无法访问服务器端信息。 (And thank goodness!) However, if you wanted to, there is nothing to stop you from: (感谢谢天谢地!)但是,如果您愿意,没有什么可以阻止您:

  1. Creating a page in some server-side scripting language that either: 用某种服务器端脚本语言创建页面,该页面可以:
    A. Grabbed the contents of all of the .css files inside of site/libraries/folder and served them up as one CSS file upon request. A.在site/libraries/folder内获取所有.css文件的内容,并根据请求将它们作为一个CSS文件提供。
    B. Sent your client-side script a list of all of the names of the .css files in the folder so it can load them when needed. B.向您的客户端脚本发送文件夹中所有.css文件名称的列表,以便它可以在需要时加载它们。
  2. Set up a script in your deployment phase that either: 在部署阶段中设置脚本,该脚本可以:
    A. Updates your .js , .css , or .html files with the names of the .css files you want to use. .css用要使用的.css文件名更新.js.css.html文件。
    B. Concatenates all of your .css files into one file and deploys that to your server. B.串接所有的.css文件合并成一个文件,然后部署到服务器。

Each of these approaches has strengths and weaknesses. 这些方法都有优点和缺点。 1A requires processing time for every request (unless you cache the results, in which case you might want to consider just going for 2B ) 1B will not work for clients with Javascript disabled. 1A需要处理每个请求的时间(除非您缓存结果,在这种情况下,您可能只考虑使用2B1B不适用于禁用Javascript的客户端。 Both 2A and 2B require that you always run your deploy scripts after you make an edit. 2A2B都要求您始终在进行编辑后运行部署脚本。 So it's really up to you. 因此,这完全取决于您。

Just an idea (not tested): 只是一个想法(未经测试):

  • setup the htaccess file to allow listing the directory contents of the stylesheets directory 设置htaccess文件以允许列出样式表目录的目录内容
  • parse the result to extract the file names 解析结果以提取文件名
  • inject the link tags using document.write 使用document.write注入链接标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM