• 动态加载javascript和css


     //动态加载js
    function loadScript(url) {
    	var script = document.createElement('script');
    	script.type = 'text/javascript';
    	script.src = url;
    	document.getElementsByTagName('head')[0].appendChild(script);
    }
    
    //动态加载css
    function loadStyles(url) {
    	var link = document.createElement('link');
    	link.rel = 'stylesheet';
    	link.type = 'text/css';
    	link.href = url;
    	document.getElementsByTagName('head')[0].appendChild(link);
    }
    
    //动态执行js
    var script = document.createElement('script');
    script.type = 'text/javascript';
    var text = document.createTextNode("alert('Lee')"); //IE 浏览器报错
    try{
    	script.appendChild(text);
    }catch(e){
        script.text = "alert('Lee')"
    }
    document.getElementsByTagName('head')[0].appendChild(script);
    
    //动态执行css
    var style = document.createElement('style');
    style.type = 'text/css';
    document.getElementsByTagName('head')[0].appendChild(style);
    insertRule(document.styleSheets[0], 'body', 'background:red', 0);
    
    function insertRule(sheet, selectorText, cssText, position) {
    	if (sheet.insertRule) {//w3c
    	    sheet.insertRule(selectorText + "{" + cssText + "}", position);
    	} else if (sheet.addRule) {//ie
    	    sheet.addRule(selectorText, cssText, position);
    	}
    }
    
  • 相关阅读:
    char/unsigned char/int/short 存储范围
    js 数字数组按大小排序
    【转】Vue生命周期
    mvn+spring+webapp模板
    【转存】Vue组件选项props
    eclipse -- git 显示修改历史 对比文件
    eclipse -- git 提示
    mysql -- 查询并插入
    git --eclipse -- 下载超时
    mysql -- 字符串长度
  • 原文地址:https://www.cnblogs.com/fangxuele/p/5430808.html
Copyright © 2020-2023  润新知