动态操作js
动态加载
在需要加载js的地方,直接写入一下代码
1
| document.write('<script src="js/jquery-3.0.0.js" type="text/javascript" charset="utf-8"><\/script>');
|
Example:
1 2 3 4 5 6 7 8
| <head> <meta charset="utf-8" /> <title></title> <script type="text/javascript"> window.jQuery || document.write('<script src="js/jquery-3.0.0.js" type="text/javascript" charset="utf-8"><\/script>'); </script> <script src="js/jquery.jsonp.js" type="text/javascript" charset="utf-8"></script> </head>
|
动态更改
<script src='' id="s1"></script>
1 2
| var s1 = document.getElementById("s1"); s1.src = "js/jquery-3.0.0.js";
|
动态创建
1 2 3 4 5
| var oHead = document.getElementsByTagName('HEAD').item(0); var oScript= document.createElement("script"); oScript.type = "text/javascript"; oScript.src="test.js"; oHead.appendChild( oScript);
|
按需加载
1
| window.jQuery || document.write('<script src="js/jquery-3.0.0.js" type="text/javascript" charset="utf-8"><\/script>');
|