如何引入(调用)一个 js文件

原文地址为: 如何引入(调用)一个 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" οnclick="start()">
<input type="button" style='font-size:40px' value="Stop" οnclick="stop();">
</form>
<br>&nbsp;&nbsp;&nbsp;&nbsp;
<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" οnclick="start()">
<input type="button" style='font-size:40px' value="Stop" οnclick="stop();">
</form>
<br>&nbsp;&nbsp;&nbsp;&nbsp;
<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" οnclick="start()">
<input type="button" style='font-size:40px' value="Stop" οnclick="stop();">
</form>
<br>&nbsp;&nbsp;&nbsp;&nbsp;
<font color="blue" style='font-size:150px' id="num"></font>
<br>
</body>
</html>

Ok


转载请注明本文地址: 如何引入(调用)一个 js文件
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值