1 CSS 样式
1.1 问题
分别使用三种方式的 CSS 样式设置如下效果。

图 - 1
1.2 步骤
实现此案例需要按照如下步骤进行。
步骤一: 通过内联 (行内) 的方式实现样式的设置
1)创建文件 css1.html
2)通过快捷方式生成页面结构
3)添加 div 标签,并通过 style 属性设置样式
1. <!DOCTYPE html>2. <head>3. <meta charset="UTF-8">4. <title>css案例</title>5. </head>6. <body>7. <!-- 行内样式 -->8. <h1 style="width: 300px; height: 50px;line-height: 50px;9. text-align: center;10. background-color:black;color:gold;">11. 《普希金文学集》12. </h1>13. </body>14. </html>
步骤二: 通过内嵌的方式实现样式的设置
1)创建文件 css2.html。
2)通过快捷方式生成页面结构
3)添加 div 标签,并设置内容
4)在 head 标签内,添加 style 标签,并设置样式
1. <!DOCTYPE html>2. <head>3. <meta charset="UTF-8">4. <title>css案例</title>5. <style>6. /\* 内嵌样式 */7. div{8. width:200px;9. height:50px;10. line-height:50px;11. text-align:center;12. background-color:lightblue;13. border-radius:10px;14. }15. </style>16. </head>17. <body>18. <div>19. 《米小圈上学记》20. </div>21. </body>22. </html>
步骤三: 通过外链的方式实现样式的设置
1)创建文件 css1.css,并添加如下内容
1. /\* 外链样式 */2. div{3. width:200px;4. height:50px;5. line-height:50px;6. text-align:center;7. background-color:lightblue;8. border-radius:10px;9. }
2)创建 css3.html 文件
3)通过快捷方式生成页面结构
4)添加 div 标签,并设置内容
5)在 head 标签内,添加 link 标签,并设置属性
1. <!DOCTYPE html>2. <head>3. <meta charset="UTF-8">4. <title>css案例</title>5. </head>6. <body>7. <!-- 行内样式 -->8. <h1 style="width: 300px; height: 50px;line-height: 50px;9. text-align: center;10. background-color:black;color:gold;">11. 《普希金文学集》12. </h1>13. </body>14. </html>
2 JavaScript 的使用方式
2.1 问题
通过三种不同的 JavaScript 方式,弹框显示 “hello, 北京 “。
2.2 步骤
实现此案例需要按照如下步骤进行。
步骤一: 通过元素绑定事件的方式实现弹框
1)创建文件 js1.html
2)通过快捷方式生成页面结构
3)添加 button 标签,并添加 onclick 事件
1. <!DOCTYPE html>3. <head>4. <meta charset="UTF-8">5. <title>js案例</title>6. </head>7. <body>8. <button onclick="alert('hello,北京')">click me</button>9. </body>10. </html>
步骤二: 通过内嵌的方式实现弹框
1)创建文件 js2.html。
2)通过快捷方式生成页面结构
3)在 head 标签内,添加 script 标签,并添加 JavaScript 代码
1. <!DOCTYPE html>2. <head>3. <meta charset="UTF-8">4. <title>js案例</title>5. <script>6. alert("hello,北京");7. </script>8. </head>9. <body>11. </body>12. </html>
步骤三: 通过外链的方式实现弹框
1)创建文件 js.js,并添加如下内容
1. /\* 外链样式 */2. alert("hello,北京");
2)创建 js3.html 文件
3)通过快捷方式生成页面结构
4)添加 script 标签,并设置 src 属性链接外部 js 文件
1. <!DOCTYPE html>2. <head>3. <meta charset="UTF-8">4. <title>js案例</title>5. <script src="js.js"></script>6. </head>7. <body>9. </body>10. </html>
3 JavaScript 输入输出
3.1 问题
接收输入的用户名,并在页面中输出显示。
3.2 步骤
1)创建文件 js4.html
2)通过快捷方式生成页面结构
3)添加 script 标签,在标签中添加代码
1. <!DOCTYPE html>2. <head>3. <meta charset="UTF-8">4. <title>js案例</title>5. <script>6. name = prompt("请输入您的姓名:");7. document.write("欢迎您,"+name);8. </script>9. </head>10. <body>12. </body>13. </html>
4 运算符
4.1 问题
接收一个年份的输入,判断是否是闰年。
4.2 步骤
1)创建文件 js5.html
2)通过快捷方式生成页面结构
3)添加 script 标签,在标签中添加代码
1. <!DOCTYPE html>2. <head>3. <meta charset="UTF-8">4. <title>js案例</title>5. <script>6. year = prompt("请输入年份:");7. if (year%4==0&&year%100!=0||year%400==0){8. document.write(year+"是闰年");9. }else{10. document.write(year+"是平年");11. }12. </script>13. </head>14. <body>15. </body>16. </html>
5 选择结构
5.1 问题
输入成绩值,范围是 1~100 ,输出优良中差四个等级。
5.2 步骤
1)创建文件 js6.html
2)通过快捷方式生成页面结构
3)添加 script 标签,在标签中添加代码
1. <!DOCTYPE html>2. <head>3. <meta charset="UTF-8">4. <title>js案例</title>5. <script>6. score = prompt("请输入成绩(0~100):");7. if (score<=100 && score>=90){8. document.write("成绩优秀");9. }else if(score<90 && score>=80){10. document.write("成绩良好");11. }else if(score<80 && score>=60){12. document.write("成绩中等");13. }else if(score<60 && score>=0){14. document.write("成绩较差");15. }else{16. document.write("成绩有误");17. }18. </script>19. </head>20. <body>21. </body>22. </html>
6 循环结构
6.1 问题
计算 1~100 的和。
6.2 步骤
1)创建文件 js7.html
2)通过快捷方式生成页面结构
3)添加 script 标签,在标签中添加代码
1. <!DOCTYPE html>2. <head>3. <meta charset="UTF-8">4. <title>js案例</title>5. <script>6. var sum=0;7. for(var i=0;i<=100;i++){8. sum+=i;9. }10. alert("sum="+sum);11. </script>12. </head>13. <body>14. </body>15. </html>
7 对象的使用
7.1 问题
定义一个手机对象,包含品牌和颜色属性,打电话和拍照方法。
7.2 步骤
1)创建文件 js8.html
2)通过快捷方式生成页面结构
3)添加 script 标签,在标签中添加代码
1. <!DOCTYPE html>2. <head>3. <meta charset="UTF-8">4. <title>js案例</title>5. <script>6. var phone={7. brand:"华为",8. color:"黑色",9. ShowInfo:function(){10. alert("我是用的是"+this.brand+"手机,颜色是"+this.color);11. }12. };13. phone.ShowInfo();14. </script>15. </head>16. <body>17. </body>18. </html>
8 DOM 操作
8.1 问题
接收用户在表单中输入的姓名和年龄,并在页面中输出。
8.2 步骤
1)创建文件 js9.html
2)通过快捷方式生成页面结构
3)添加表单元素和表单控件
4)添加 script 标签,在标签中添加代码
1. <!DOCTYPE html>2. <head>3. <meta charset="UTF-8">4. <title>js案例</title>6. </head>7. <body>8. <form action="" method="post">9. <p>10. 姓名:<input type="text" id="name">11. </p>12. <p>13. 密码:<input type="password" id="password">14. </p>15. <p>16. <input type="button" value="显示" onclick="show()">17. </p>18. </form>19. <script>20. function show(){21. var name=document.getElementById("name").value;22. var password=document.getElementById("password").value;23. document.write("姓名:"+name,",密码"+password);24. }26. </script>27. </body>28. </html>
https://tts.tmooc.cn/ttsPage/NTD/NTDTN202109/WEBBASE/DAY02/CASE/01/index.html
