成熟丰满熟妇高潮XXXXX,人妻无码AV中文系列久久兔费 ,国产精品一国产精品,国精品午夜福利视频不卡麻豆

您好,歡迎來到九壹網(wǎng)。
搜索
您的當(dāng)前位置:首頁不使用script導(dǎo)入js文件的幾種方法

不使用script導(dǎo)入js文件的幾種方法

來源:九壹網(wǎng)

方法一:原生

 adc.js內(nèi)容如下:

var hello = "H9";

html.html

<script>
 var s = document.createElement("script");
 s.src = "abc.js";
 document.head.appendChild(s);
 s.addEventListener("load",function(){
 // 等待s的load事件加載完響應(yīng),防止未加載完就調(diào)用出錯
 console.log(hello);
 })
 
 setTimeout(function(){//或者使用定時器保證其載入完后調(diào)用(不安全,不如監(jiān)聽事件好)
 console.log(hello);
 },1000);
 // $.getScript("abc.js");
 </script>

方法二:jquery.js

$.getScript("abc.js",function(){ alert("heheheh"); console.log(hello); });
<script type="text/javascript" src="../jquery.js"></script>
<script type="text/javascript">
$(function()
{
$('#loadButton').click(function(){
$.getScript('new.js',function(){
newFun('"Checking new script"');//這個函數(shù)是在new.js里面的,當(dāng)點擊click后運(yùn)行這個函數(shù)
});
});
});
</script>
</head>
<body>
<button type="button" id="loadButton">Load</button>

方法三:require.js

require.js分享2.1.1版本,注意是針對大項目使用,一邊情況下使用jquery即可。

index.html

<!--設(shè)置入口文件main 可以省略js-->
<script data-main="main" src="require.js"></script>

main.js

console.log("你好世界");
require(["js1","js2","js3"],function () {
 // 是異步加載導(dǎo)入。js后綴可以省略
 console.log("你們加載完了么?");
 var total = num1+num2+num3;
 console.log(total);
 hello1();
 hello2();
 hello3();
})

使用requireJs可以很方便的導(dǎo)入js文件,但是要注意js文件中變量名方法名沖突的問題。 產(chǎn)生原因:瀏覽器js文件共用全局作用域,作用域中變量名方法名可能被覆蓋

Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號-2

違法及侵權(quán)請聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市萬商天勤律師事務(wù)所王興未律師提供法律服務(wù)