【代码】使用JavaScript动态引用其他Js和CSS文件

2021-11-25 15:47:48  阅读 2182 次 评论 0 条

直接上代码

var importCssJs = {
	css: function(path) {
		if(!path || path.length === 0) {
			throw new Error('参数"path"错误');
		}
		var head = document.getElementsByTagName('head')[0];
		var link = document.createElement('link');
		link.href = path;
		link.rel = 'stylesheet';
		link.type = 'text/css';
		head.appendChild(link);
	},
	js: function(path) {
		if(!path || path.length === 0) {
			throw new Error('参数"path"错误');
		}
		var head = document.getElementsByTagName('head')[0];
		var script = document.createElement('script');
		script.src = path;
		script.type = 'text/javascript';
		head.appendChild(script);
	}
}

调用:

importCssJs.css('css/main.css');
importCssJs.js('js/main.js');



微信扫码查看本文
本文地址:https://www.yangguangdream.com/?id=2184
版权声明:本文为原创文章,版权归 编辑君 所有,欢迎分享本文,转载请保留出处!

发表评论


表情

还没有留言,还不快点抢沙发?