• 前端作业(17)


    之后的20个作业,学自【20个JavaScript经典案例-哔哩哔哩】 https://b23.tv/kVj1P5f,加上自己学习的知识组合

    支付倒计时

    1. 支付10s倒计时

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>支付10s倒计时</title>
    8. <style>
    9. div {
    10. width: 200px;
    11. height: 200px;
    12. background-color: #eee;
    13. padding: 20px;
    14. margin: 0 auto;
    15. }
    16. button {
    17. margin: 30px 25px;
    18. }
    19. </style>
    20. </head>
    21. <body>
    22. <div>
    23. <table>
    24. <tr>
    25. <td>商品:</td>
    26. <td>Web前端课程</td>
    27. </tr>
    28. <tr>
    29. <td>原价:</td>
    30. <td>1980</td>
    31. </tr>
    32. <tr>
    33. <td>现价:</td>
    34. <td>1.98</td>
    35. </tr>
    36. <tr>
    37. <td>内容:</td>
    38. <td>HTML</td>
    39. </tr>
    40. <tr>
    41. <td>地址:</td>
    42. <td>北京朝阳区</td>
    43. </tr>
    44. <tr></tr>
    45. <tr>
    46. <td><button>取消</button></td>
    47. <td><button>确定</button></td>
    48. </tr>
    49. <tr></tr>
    50. </table>
    51. </div>
    52. <script>
    53. // 用getElementsByTagName()方法获取button,因为支付是第二个,所以后跟[1],数组从0开始计数,所以是1
    54. // 1. 声明范围:
    55. // let块作用域
    56. // if (true) {
    57. // let a1 = 'ss';
    58. // console.log(a1); // ss
    59. // }
    60. // console.log(a1); // ReferenceError
    61. // var函数作用域
    62. // if (true) {
    63. // var a1 = 'ss';
    64. // console.log(a1); // ss
    65. // }
    66. // console.log(a1); //ss
    67. // 2. let定义变量不能预解析,提前调用会报错 ReferenceError; var相反,可以预解析,结果为 undefined
    68. // console.log(a1); // ReferenceError
    69. // console.log(a2); // undefined
    70. // let a1 = 'ss';
    71. // var a2 = 'as';
    72. document.getElementsByTagName('button')[1].onclick = function() {
    73. let res = window.confirm('您确定吗?');
    74. if (res) {
    75. location.href = 'payment.html';
    76. }
    77. }
    78. </script>
    79. </body>
    80. </html>

     ① 常用输入输出语句

    alert() = window.alert()浏览器弹出警示框
    prompt()浏览器弹出输入框
    console.log()浏览器控制台输出信息
    document.write()HTML中输入信息

    ② document 对象,对HTML中的元素进行访问

    body对 body 元素直接访问
    cookie设置或返回文档相关的所有cookie
    domain返回文档域名
    title返回文档标题
    URL返回文档URL
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <script>
    11. console.log(document.body.scrollWidth);
    12. console.log(document.cookie);
    13. console.log(document.domain);
    14. console.log(document.title);
    15. console.log(document.URL);
    16. </script>
    17. </body>
    18. </html>

    write()写入HTML表达式或JS代码
    getElementById()返回指定id的第一个对象的引用
    getElementByName()返回指定名称的对象集合
    getElementByTagName()返回指定标签名对象集合
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <script>
    11. document.write('I am');
    12. function reWrite() {
    13. document.write('rew');
    14. }
    15. </script>
    16. <button onclick="reWrite()">onclick</button>
    17. </body>
    18. </html>

     

    try1

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. <style>
    9. div {
    10. margin: 0 auto;
    11. width: 500px;
    12. }
    13. #sec {
    14. color: red;
    15. font-size: 30px;
    16. }
    17. </style>
    18. </head>
    19. <body>
    20. <div>
    21. <h2>恭喜您,支付成功!</h2>
    22. <p><span id="sec">10</span>s后自动返回首页</p>
    23. <button>立即返回</button>
    24. </div>
    25. <script>
    26. window.onload = function() {
    27. let timer = 10;
    28. setInterval(() => {
    29. timer--;
    30. document.getElementById('sec').innerText = timer;
    31. if (timer == 0) {
    32. location.href = 'https://www.bilibili.com';
    33. }
    34. }, 1000);
    35. }
    36. document.getElementsByTagName('button')[0].onclick = function() {
    37. location.href = 'https://www.bilibili.com';
    38. }
    39. </script>
    40. </body>
    41. </html>

    ③ window 对象表浏览器的一个实例,双重角色,即JS访问浏览器窗口的一个接口和ECMAScript的全局对象。

    (1)window对象是BOM(Browser Object Model)的核心对象,BOM其他对象,如document、location、navigator、screen、history,就是window对象的子对象。

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <script>
    11. var age = 18;
    12. console.log(age); //18
    13. console.log(window.age); //18
    14. function sayAge() {
    15. console.log(age);
    16. }
    17. sayAge(); //18
    18. window.sayAge(); //18
    19. // 全局定义了变量 age 和函数 sayAge(),
    20. // 则它们自动转为window对象的属性或方法,
    21. // window.age和window.sayAge()可以访问age和sayAge()
    22. </script>
    23. </body>
    24. </html>

     

     (2)window对象中两个函数

    setTimeout(code/function,milliseconds);实现一个函数在指定的时间(毫秒)之后运行几毫秒后运行
    setInterval(code/function,milliseconds);间隔几毫秒就重复运行
    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. </head>
    9. <body>
    10. <script>
    11. setTimeout(function() {
    12. console.log('hello');
    13. }, 1000);
    14. setInterval(function() {
    15. console.log('hello');
    16. }, 1000);
    17. var timer = setInterval(function() {
    18. console.log('hi');
    19. }, 2000);
    20. clearInterval(timer); //取消定时器
    21. </script>
    22. </body>
    23. </html>

  • 相关阅读:
    AgeDB(data.Dataset)
    ChatGPT研究论文提示词集合3-【数据收集】、【数据分析】和【解释与讨论】
    Web3中文|10月份超48%的以太坊NFT交易额是假的
    定时器浅析
    【Gradle自动化构建编程框架】一、介绍
    《第一堂棒球课》:走进棒球运动·美职棒小联盟
    批量检查多台服务器 高亮打印每个IP 绿底红字
    英语考试的作文模板
    工厂设计模式
    【JavaScript】video标签配置及相关事件:
  • 原文地址:https://blog.csdn.net/weixin_63492023/article/details/133616162