怎么引入(調(diào)用)一個(gè)JS文件
我們旺旺需要調(diào)用別的 js文件。怎么處理?
看隨機(jī)抽取這個(gè)例子。在一個(gè)頁面中如下:
<html> <head> <title>random number</title> <script type="text/javascript"> //隨機(jī)抽取人名 </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 放在另外一個(gè)文件里,比如當(dāng)前文件夾的 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); }
這樣就行了。
當(dāng)然,也可以把 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>
以上所述是小編給大家介紹的引入(調(diào)用)一個(gè)JS文件的方法,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持
相關(guān)文章
詳解PHP中pathinfo()函數(shù)導(dǎo)致的安全問題
這篇文章主要給大家介紹了PHP中pathinfo()函數(shù)導(dǎo)致的安全問題,文中給出了詳細(xì)的介紹與示例代碼,相信對(duì)大家的理解和學(xué)習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下,下面來一起看看吧。2017-01-01JavaScript Base64 作為文件上傳的實(shí)例代碼解析
這篇文章主要介紹了JavaScript Base64 作為文件上傳的實(shí)例代碼解析,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-02-02antDesign 自定義分頁樣式的實(shí)現(xiàn)代碼
這篇文章主要介紹了antDesign 自定義分頁樣式的實(shí)現(xiàn)代碼,這里用到了自定義指令,如果大家用不到可以按照自己的實(shí)際效果開發(fā),本文通過實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下2022-10-10