zoukankan      html  css  js  c++  java
  • 动态加载js和css

    // 动态加载外部js文件
    var flag = true;
    if( flag ){
    	loadScript( "js/index.js" );
    };
    function loadScript( url ){
    	var script = document.createElement( "script" );
    	script.type = "type/javascipt";
    	script.src = url;
    	document.getElementsByTagName( "head" )[0].appendChild( script );
    };
    
    // 动态加载js
    if( flag ){
    	var script = document.createElement( "script" );
    	script.type = "text/javascript";
    	script.text = " ";
    	document.getElementsByTagName( "head" )[0].appendChild( script );
    };
    
    // 动态加载外部css样式
    if( flag ){
    	loadCss( "css/base.css" );
    };
    function loadCss( url ){
    	var link = document.createElement( "link" );
    	link.type = "text/css";
    	link.rel = "stylesheet";
    	link.href = url;
    	document.getElementsByTagName( "head" )[0].appendChild( link );
    };
    
    // 动态加载css样式
    if( flag ){
    	var style = document.createElement( "style" );
    	style.type = "text/css";
    	document.getElementsByTagName( "head" )[0].appendChild( style );
    	var sheet = document.styleSheets[0];
    	insertRules( sheet,"#gaga1","background:#f00",0 );
    };
    function insertRules( sheet,selectorTxt,cssTxt,position ){
    	if( sheet.insertRule ){ // 判断非IE浏览器
    		sheet.insertRule( selectorTxt + "{" + cssTxt +"}" ,position );
    	}else if( sheet.addRule ){ //判断是否是IE浏览器
    		sheet.addRule( selectorTxt ,cssTxt ,position )
    	}
    }
    


  • 相关阅读:
    堆和栈 的区别
    equals == 区别
    【知识点】Filter、Servlet、Listener区别与联系
    白盒测试相关的一些知识
    紧急情况下压缩了测试周期应该怎么办?
    软件性能测试与可靠性测试
    软件测试概念
    web测试方法总结
    结对测试探讨
    八种状态增加测试用例状态的精确度
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3024734.html
Copyright © 2011-2022 走看看