怎么引入(調用)一個JS文件
更新時間:2016年05月26日 11:38:15 作者:allback
這篇文章主要介紹了引入(調用)一個JS文的方法,非常不錯介紹的非常詳細,具有參考借鑒價值,感興趣的朋友一起看看吧
我們旺旺需要調用別的 js文件。怎么處理?
看隨機抽取這個例子。在一個頁面中如下:
<html> <head> <title>random number</title> <script type="text/javascript"> //隨機抽取人名 </script> </head> <body> <form> <input type="button" style='font-size:40px' value="Start" onclick="start()"> <input type="button" style='font-size:40px' value="Stop" onclick="stop();"> </form> <br> <font color="blue" style='font-size:150px' id="num"></font> <br> </body> </html>
我們可以把 js 放在另外一個文件里,比如當前文件夾的 a.js 中。
這樣 html 頁面如下:
<html> <head> <title>random number</title> <script type="text/javascript" src="a.js"> </script> </head> <body> <form> <input type="button" style='font-size:40px' value="Start" onclick="start()"> <input type="button" style='font-size:40px' value="Stop" onclick="stop();"> </form> <br> <font color="blue" style='font-size:150px' id="num"></font> <br> </body> </html>
a.js
var errorString = "Please input a positive integer.";
var arr = ["A", "B", "C", "D"];
function count() {
max = arr.length; //max, 全局變量
document.getElementById("num").innerHTML = arr[parseInt(max * Math.random())];
}
function start()
{
timeId = setInterval("count();", 100);
}
function stop() {
clearInterval(timeId);
}
這樣就行了。
當然,也可以把 a.js放在web上,然后引用成下面這樣。
<html> <head> <title>random number</title> <script type="text/javascript" src="http://localhost:8080/test/js/random1.js"></script> </head> <body> <form> <input type="button" style='font-size:40px' value="Start" onclick="start()"> <input type="button" style='font-size:40px' value="Stop" onclick="stop();"> </form> <br> <font color="blue" style='font-size:150px' id="num"></font> <br> </body> </html>
以上所述是小編給大家介紹的引入(調用)一個JS文件的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持
相關文章
關于IE瀏覽器以及Firefox下的javascript冒泡事件的響應層級
原來是由于IE瀏覽器以及Firefox對于冒泡型事件的支持層次不同造成的。(如對冒泡事件不是很了解可先查詢相關資料)2010-10-10
用JavaScript實現(xiàn)用一個DIV來包裝文本元素節(jié)點
當我試圖將文本(可能也包含HTML元素)用一個DIV元素包起來時,可以使用下面的方法,需要的朋友可以參考下2014-09-09

