动态加载js和css

2017-02-16 15:14:14

开发过程中经常需要动态加载js和css,今天特意总结了一下常用的方法。

 1、动态加载js

  方法一:动态加载js文件

复制代码

1     // 动态加载js脚本文件2     function loadScript(url) {3         var script = document.createElement("script");4         script.type = "text/javascript";5         script.src = url;6         document.body.appendChild(script);7     }8     // 测试9     loadScript("javascript/lib/cookie.js");

复制代码

  方法二:动态加载js脚本

复制代码

 1    // 动态加载js脚本 2     function loadScriptString(code) { 3         var script = document.createElement("script"); 4         script.type = "text/javascript"; 5         try{ 6             // firefox、safari、chrome和Opera 7             script.appendChild(document.createTextNode(code)); 8         }catch(ex) { 9             // IE早期的浏览器 ,需要使用script的text属性来指定javascript代码。10             script.text = code;11         }12         document.body.appendChild(script);13     }14     // 测试15     var text = "function test(){alert('test');}";16     loadScriptString(text);17     test();

复制代码

2、动态加载css

  方法一:动态加载css文件

复制代码

 1     // 动态加载css文件 2     function loadStyles(url) { 3         var link = document.createElement("link"); 4         link.type = "text/css"; 5         link.rel = "stylesheet"; 6         link.href = url; 7         document.getElementsByTagName("head")[0].appendChild(link); 8     } 9     // 测试10     loadStyles("css/secondindex.css");

复制代码

  方法二:动态加载css脚本

复制代码

 1    // 动态加载css脚本 2     function loadStyleString(cssText) { 3         var style = document.createElement("style"); 4         style.type = "text/css"; 5         try{ 6             // firefox、safari、chrome和Opera 7             style.appendChild(document.createTextNode(cssText)); 8         }catch(ex) { 9             // IE早期的浏览器 ,需要使用style元素的stylesheet属性的cssText属性10             style.styleSheet.cssText = cssText;11         }12         document.getElementsByTagName("head")[0].appendChild(style);13     }14     // 测试15     var css = "body{color:blue;}";16     loadStyleString(css);

复制代码

 


  • 2019-10-15 09:20:49

    SpringBoot注解梳理

    @SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。其中@ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文。 @Configuration 等同于spring的XML配置文件;使用Java代码可以检查类型安全。 @EnableAutoConfiguration 自动配置。 @ComponentScan 组件扫描,可自动发现和装配一些Bean。 @Component可配合CommandLineRunner使用,在程序启动后执行一些基础任务。 @RestController注解是@Controller和@ResponseBody的合集,表示这是个控制器bean,并且是将函数的返回值直 接填入HTTP响应体中,是REST风格的控制器。 @Autowired自动导入。 @PathVariable获取参数。 @JsonBackReference解决嵌套外链问题。 @RepositoryRestResourcepublic配合spring-boot-starter-data-rest使用。

  • 2019-10-15 09:52:00

    动图解释递归,按值传递和按引用传递的区别,线性查找和二分查找,二叉查找树

    对于大部分人,数据结构一直是一个短板,当然我也是,不是学不会,而是容易忘,就拿最简单的排序来说吧,当时学习的时候明明已经弄得很清楚了,过了一段时间不用又忘记了,还要重新再看一遍,不知道有多少小伙伴和我有一样的烦恼。今天让我们用用动图的方式学习一下数据结构中的递归和二分查找吧,这种讲解方式非常生动,而且非常容易记住和理解。

  • 2019-10-16 21:02:47

    vue中mixins的详细分析一

    混入 (mixins): 是一种分发 Vue 组件中可复用功能的非常灵活的方式。混入对象可以包含任意组件选项。当组件使用混入对象时,所有混入对象的选项将被混入该组件本身的选项。

  • 2019-10-16 21:04:47

    vue中mixins的详细分析二

    当混合里面包含异步请求函数,而我们又需要在组件中使用异步请求函数的返回值时,我们会取不到此返回值,如下: