JS,CSS文件引入代码封装

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>JS,CSS文件引入代码封装</title>
</head>
<body>
    <script type="text/javascript">

        var vars = {
            jsPath: 'resources/script/',
            jsTmp: 'resources/script/artTemplate/',
            cssPath: 'resources/skin/',
            isLayer: 'resources/script/layer/'
        };

//扩展
var fnExtend = {
    includFile: function(g, c) {
        for (var f = "string" == typeof c ? [c] : c, d = 0; d < f.length; d++) {
            var b = f[d].replace(/^\s|\s$/g, ""),
                a = b.split("."),
                e = "css" == a[a.length - 1].toLowerCase(),
                a = e ? "link" : "script",
                h = e ? " type='text/css' rel='stylesheet' " : " language='javascript' type='text/javascript' ",
                b = (e ? "href" : "src") + "='" + g + b + "'";

                console.log("<" + a + h + b + "></" + a + ">");
            //0 == $(a + "[" + b + "]").length && document.write("<" + a + h + b + "></" + a + ">")
        }
    }
};

//插入css文件
fnExtend.includFile(vars.cssPath, ['base.css', 'content.css','blue.css']);
//插入js文件
fnExtend.includFile(vars.jsPath, ['jquery-ui.min.js','dcselect.js']);
fnExtend.includFile(vars.isLayer, ['layer.js']);
fnExtend.includFile(vars.jsTmp, ['template.js']);

    </script>
</body>
</html>

浏览器控制台输出:

这里写图片描述

在编写插件的时候,需要引入css,js文件的时候,只需要维护vars json对象,调用fnExtend.includFile函数即可,还是比较方便,代码比较简单,通过判断后缀是css还是js拼接字符串操作而已。

猜你喜欢

转载自blog.csdn.net/huangbaokang/article/details/80135752
今日推荐