怎么引入(调用)一个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文件的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持
热门推荐
1 如何训练狗狗不叫
2 训练狗狗不叫
3 训练狗狗随行
4 怎么训练狗狗不叫
7 训练比熊狗狗教程
10 上海狗狗训练
11 怎么训练狗狗上厕所
12 训练狗狗趴下
13 狗狗服从训练
14 训练狗狗拒食
15 训练狗狗笑
16 训练狗狗大便
17 训练狗狗立
18 训练狗狗捡东西