js动态引入css

1、获取到js的文件夹目录

2、插入到head中

 

ready: function(){
	var that = this;

	that.link();
},
getPath: function(){
	var jsPath = document.currentScript ? document.currentScript.src : function(){
		var js = document.scripts
		,last = js.length - 1
		,src;
		for(var i = last; i > 0; i--){
			if(js[i].readyState === 'interactive'){
				src = js[i].src;
				break;
			}
		}
		return src || js[last].src;
	}();
	return jsPath.substring(0, jsPath.lastIndexOf('/') + 1);
},
link: function(){
	var that = this;

	var head = document.getElementsByTagName('head')[0];
	var link = document.createElement('link');
	var path = that.getPath();
	var href = 'theme/default/style.css?t=' + that.version;
	
	link.rel = 'stylesheet';
	link.href = path + href;

	head.appendChild(link);
}

 

你可能感兴趣的:(码农时间)