zoukankan      html  css  js  c++  java
  • 动态加载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);
    	}
    }
    
  • 相关阅读:
    mergeKLists
    generateParenthesis
    removeNthFromEnd
    Codeforces Round #632 (div.2) C. Eugene and an array
    Spring中@Import的三种情况
    自定义Spring Boot starter
    Java 注解
    Eclipse安装Lombok插件
    java 类加载系统
    Centos系统中忘了root密码怎么办
  • 原文地址:https://www.cnblogs.com/fangxuele/p/5430808.html
Copyright © 2011-2022 走看看