html整体引入js,html页面用js引入js的方式
最原始的是用script便簽:
1. 使用js打印這個便簽:
當然這也可以動態引入css以及其他html元素。
2. 使用dom的api添加script元素:
jQuery中封裝了這種方式,并貼心的加入了回調:
$.getScript(url,callback(res, status));
簡單實現如下:
var getScript = function(url, callback) {
var js = document.createElement('script');
js.setAttribute('type', 'text/javascript');
js.setAttribute('src', url);
document.getElementsByTagName('head')[0].appendChild(js);
if(callback && typeof callback == 'function'){
if (document.all) { //IE
js.onreadystatechange = function() {
if(js.readyState == 'loaded' || js.readyState == 'complete'){
callbackFn();
}
}
}else{
js.onload = function() {
callbackFn();
}
}
}
}
總結
以上是生活随笔為你收集整理的html整体引入js,html页面用js引入js的方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c++代码根据点位连线_邹军:数控车倒角
- 下一篇: 数据消费过程_特色鲜卤现捞加盟店经营要如