怎么引入(调用)一个JS文件
2019/6/27 22:24:07
本文主要是介绍怎么引入(调用)一个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文件的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对找一找教程网网站的支持
这篇关于怎么引入(调用)一个JS文件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist
- 2024-03-11icons for vue
- 2024-03-07breadcrumbs react js
- 2024-03-06react login page example
- 2024-03-06react router uselocation
- 2024-03-04postgres jsonb_set
- 2024-03-01react native uuid
- 2024-02-29vuejs sidebar