Blogger Information
Blog 70
fans 4
comment 5
visits 104888
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JavaScript:时间对象,实例演示右下角广告图
JiaJieChen
Original
947 people have browsed it

JavaScript:时间对象,实例演示右下角广告图

一.时间对象实例

方法 含义
new Date() 获取时间
getDate() 获取日数
getMonth() 获取月份
getFullYear() 获取年份
getHours() 获取小时
getMinutes() 获取分钟数
getSeconds() 获取秒数
getDay() 获取星期几

代码块

  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>获取年月日</title>
  8. <style>
  9. :root {
  10. height: 2000px;
  11. }
  12. .box {
  13. background-color: lightblue;
  14. height: 40px;
  15. width: 250px;
  16. display: flex;
  17. justify-content: center;
  18. align-items: center;
  19. color: #666;
  20. position: fixed;
  21. right: 0;
  22. bottom: 80px;
  23. }
  24. .box > nav {
  25. margin-right: 1em;
  26. }
  27. .box > button {
  28. position: absolute;
  29. right: 0;
  30. top: 0;
  31. }
  32. .box > div {
  33. position: fixed;
  34. top: auto;
  35. bottom: 0;
  36. }
  37. </style>
  38. </head>
  39. <body>
  40. <div class="box">
  41. <nav></nav>
  42. <span></span>
  43. <button>x</button>
  44. <div>
  45. <a href=""
  46. ><img
  47. src="http://imga3.5054399.com/upload_pic/2021/4/21/4399_09142332383.jpg"
  48. alt=""
  49. /></a>
  50. </div>
  51. </div>
  52. <script>
  53. //get 是获取 set 是设置
  54. let d = new Date();
  55. // getDate 获取日数
  56. let myDay = d.getDate();
  57. //getMonth 获取月份
  58. let myMonth = d.getMonth() + 1;
  59. //getFullYear 获取年份
  60. let myYear = d.getFullYear();
  61. //getHours 获取小时
  62. let myHours = d.getHours();
  63. //getMinutes 获取分钟数
  64. let myMinutes = d.getMinutes();
  65. //getSeconds 获取秒数
  66. let mySeconds = d.getSeconds();
  67. //getDay 获取星期几
  68. let myGetday = d.getDay();
  69. let week = [
  70. "星期一",
  71. "星期二",
  72. "星期三",
  73. "星期四",
  74. "星期五",
  75. "星期六",
  76. "星期日",
  77. ];
  78. // 获取.box 内的nav 元素
  79. document
  80. .querySelector(".box > nav")
  81. .append(myYear + "年" + myMonth + "月" + myDay + "日");
  82. // 获取.box 内的span 元素
  83. document.querySelector(".box > span").append(week[myGetday]);
  84. //按钮添加功能 remove 移除掉 box盒子 等于关闭掉这个提示
  85. let box = document.querySelector(".box");
  86. let btn = document.querySelector(".box > button");
  87. btn.addEventListener("click", (ev) => {
  88. ev.target = box.remove();
  89. });
  90. //判断当前时间点,输出 早上好 晚上好 下午好
  91. if (myHours < 12) {
  92. document.body.append("早上好");
  93. } else if (myHours >= 12 && myHours < 18) {
  94. document.body.append("下午好");
  95. } else {
  96. document.body.append("晚上好");
  97. }
  98. </script>
  99. </body>
  100. </html>
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post