需要准备的材料分别有:电脑、浏览器、html编辑器。
1、首先,打开html编辑器,新建html文件,例如:index.html,编写问题基础代码。
2、新建index.js文件,将内嵌js内容添加到index.js文件中。
3、在index.html的head标签中新增引用代码:<script src="index.js"></script>。
4、浏览器运行index.html页面,此时成功引入index.js并调用其内容。
js内嵌式一定要用到alert。js执行的时候,获取json过程中,没有等待,一直往下走,走到for这里,没有拿到返回的json,这个option还没有被append,就是空,里面就不会执行,加了alert,强制等待,这时候json已经有数据了,会执行到for里面的代码。实际使用中,不能一直用alert来代替,是AJAX的异步请求原理,默认是异步的,就是执行提交的时候继续往下走,要把这个默认的设置更改一下。1、在页面中直接嵌入JavaScript代码<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script type="text/javascript">
alert("helloworld")
</script>
</head>
<body>
2、链接外部JavaScript文件。
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script type="text/javascript" src="file1.js"></script>
<script type="text/javascript">
alert("helloworl")
</script>
</head>
<body>
</body>
</html>