简体   繁体   English

如何将带有 css 和 js 的完整库加载到 html?

[英]How to load a full library with css and js to html?

Say i wrote some local library that contains js and css files (no html).假设我写了一些包含jscss文件(无 html)的本地库。 This library has a js file with a class that get a DOM id as argument and load all css into that element independently.这个库有一个带有classjs文件,该类将 DOM id作为参数并将所有css独立加载到该元素中。

I would like "share" that library, or just include it in my html page.我想“分享”那个库,或者只是将它包含在我的html页面中。

The only way I know currently, is to load each file from this folder separately like:我目前知道的唯一方法是分别从该文件夹加载每个文件,例如:

<link rel="stylesheet"  href="/lib/style.css"/>
<script src="lib/main.js"></script>

and so on.. which looks not clean and not right.等等..看起来不干净也不对。

What is the right way to be able to load this library as a whole and everything inside , or at least all js inside, into an html page?能够将这个库作为一个整体以及里面的所有内容或至少里面的所有js加载到html页面中的正确方法是什么?

If I share this library, I don't want other devs to list and load each js/css .如果我共享这个库,我不希望其他开发者列出并加载每个js/css

You can use JavaScript to load CSS.您可以使用 JavaScript 加载 CSS。 Placing your script tag above the header section will load the CSS before your actual site.将您的脚本标记放在标头部分上方将在您的实际站点之前加载 CSS。

Use a hosting like pastebin for some temporary CSS file hosting.使用像pastebin这样的托管来托管一些临时的 CSS 文件。

May not load the CSS fast but at the moment it's the solution I use for sharing my libraries.可能无法快速加载 CSS,但目前它是我用来共享我的库的解决方案。

<script>
         
        // Get HTML head element
        var head = document.getElementsByTagName('HEAD')[0];
 
        // Create new link Element
        var link = document.createElement('link');
 
        // set the attributes for link element
        link.rel = 'stylesheet';
     
        link.type = 'text/css';
     
        link.href = 'INSERTYOURLINKHERE';
 
        // Append link element to HTML head
        head.appendChild(link);
    </script>

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

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