怎么引入(调用)一个JS文件
我们旺旺需要调用别的js文件。怎么处理?
看随机抽取这个例子。在一个页面中如下:
<html> <head> <title>randomnumber</title> <scripttype="text/javascript"> //随机抽取人名</script> </head> <body> <form> <inputtype="button"style='font-size:40px'value="Start"onclick="start()"> <inputtype="button"style='font-size:40px'value="Stop"onclick="stop();"> </form> <br> <fontcolor="blue"style='font-size:150px'id="num"></font> <br> </body> </html>
我们可以把js放在另外一个文件里,比如当前文件夹的a.js中。
这样html页面如下:
<html> <head> <title>randomnumber</title> <scripttype="text/javascript"src="a.js"> </script> </head> <body> <form> <inputtype="button"style='font-size:40px'value="Start"onclick="start()"> <inputtype="button"style='font-size:40px'value="Stop"onclick="stop();"> </form> <br> <fontcolor="blue"style='font-size:150px'id="num"></font> <br> </body> </html>
a.js
varerrorString="Pleaseinputapositiveinteger.";
vararr=["A","B","C","D"];
functioncount(){
max=arr.length;//max,全局变量
document.getElementById("num").innerHTML=arr[parseInt(max*Math.random())];
}
functionstart()
{
timeId=setInterval("count();",100);
}
functionstop(){
clearInterval(timeId);
}
这样就行了。
当然,也可以把a.js放在web上,然后引用成下面这样。
<html> <head> <title>randomnumber</title> <scripttype="text/javascript"src="http://localhost:8080/test/js/random1.js"></script> </head> <body> <form> <inputtype="button"style='font-size:40px'value="Start"onclick="start()"> <inputtype="button"style='font-size:40px'value="Stop"onclick="stop();"> </form> <br> <fontcolor="blue"style='font-size:150px'id="num"></font> <br> </body> </html>
以上所述是小编给大家介绍的引入(调用)一个JS文件的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语