第八节课

表单-启用&禁用文本框

  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. </head>
  9. <body>
  10. <input type="text" value="admin" placeholder="输入账号">
  11. <input type="password" value="123123">
  12. <button>禁用</button>
  13. <button>启用</button>
  14. <script>
  15. let btnList = document.getElementsByTagName('button')
  16. let input = document.getElementsByTagName('input')[0]
  17. let btn1 = btnList[0]
  18. let btn2 = btnList[1]
  19. btn1.onclick = function() {
  20. input.disabled = true
  21. }
  22. btn2.onclick = function() {
  23. input.disabled = false
  24. }
  25. </script>
  26. </body>
  27. </html>

第八节课 - 图1

表单-文本框获得失去焦距

  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. lable {
  10. display: block;
  11. }
  12. .show {
  13. display: block;
  14. }
  15. .hide {
  16. display: none;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <!-- <label for="input1">
  22. 京东: <input type="text" id="input1" value="相机">
  23. </label>-->
  24. <label for="input2">
  25. 淘宝:
  26. </label>
  27. <input type="text" id="input2" value="电脑">
  28. <!--placehoolder:
  29. <input type="text" placeholder="placeholder">-->
  30. <script>
  31. let input1 = document.getElementById('input1')
  32. let input2 = document.getElementById('input2')
  33. let lab = document.getElementsByTagName('label')[0]
  34. input2.oninput = function () {
  35. if(this.value === '') {
  36. lab.className = 'show'
  37. }else{
  38. lab.className = 'hide'
  39. }
  40. }
  41. input1.onfocus = function() {
  42. this.value = ''
  43. this.style.color = '#f00'
  44. }
  45. input1.onblur = function() {
  46. this.value = '相机'
  47. this.style.color = '#000'
  48. }
  49. </script>
  50. </body>
  51. </html>

第八节课 - 图2第八节课 - 图3

表单-注册高亮显示

  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. .right {
  10. border: 2px solid rgb(50, 158, 50);
  11. }
  12. .wrong {
  13. border: 2px solid rgb(211, 49, 49);
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. 账号:<input type="text" onblur="fn(this)">
  19. 密码:<input type="password" onblur="fn(this)">
  20. <script>
  21. function fn(a) {
  22. if(a.value.length <12 && a.value.length>8){
  23. a.className = 'right'
  24. } else {
  25. a.className = 'wrong'
  26. }
  27. }
  28. </script>
  29. </body>
  30. </html>

第八节课 - 图4第八节课 - 图5

表单-全选和反选

  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. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. .my-table {
  14. width: 500px;
  15. margin: 100px auto;
  16. }
  17. table {
  18. border-collapse: collapse;
  19. border-spacing: 0;
  20. border: 1px solid #ccc;
  21. width: 500px;
  22. }
  23. th,
  24. td {
  25. border: 1px solid #ccc;
  26. color: rgb(90, 90, 167);
  27. padding: 10px;
  28. }
  29. th {
  30. font:bold 16px "微软雅黑";
  31. background-color:#09c;
  32. color: #fff;
  33. }
  34. td {
  35. font:bold 14px "微软雅黑";
  36. }
  37. tbody tr {
  38. background-color: #f6f6f6;
  39. }
  40. tbody tr:hover {
  41. cursor: pointer;
  42. background-color: #09c;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div class="my-table">
  48. <table>
  49. <thead>
  50. <tr>
  51. <th>
  52. <input type="checkbox" id="all">
  53. </th>
  54. <th>菜名</th>
  55. <th>饭店</th>
  56. </tr>
  57. </thead>
  58. <tbody id ="content">
  59. <tr>
  60. <td>
  61. <input type="checkbox">
  62. </td>
  63. <td>菜品1</td>
  64. <td>面包店</td>
  65. </tr>
  66. <tr>
  67. <td>
  68. <input type="checkbox">
  69. </td>
  70. <td>菜品2</td>
  71. <td>烧烤店</td>
  72. </tr>
  73. <tr>
  74. <td>
  75. <input type="checkbox">
  76. </td>
  77. <td>菜品3</td>
  78. <td>奶茶店</td>
  79. </tr>
  80. <tr>
  81. <td>
  82. <input type="checkbox">
  83. </td>
  84. <td>菜品4</td>
  85. <td>火锅店</td>
  86. </tr>
  87. </tbody>
  88. </table>
  89. </div>
  90. <script>
  91. window.onload=function() {
  92. let allBtn = document.getElementById('all')
  93. let tBody = document.getElementById('content')
  94. let inputList = tBody.getElementsByTagName('input')
  95. allBtn.onclick = function() {
  96. for(let index = 0;index < inputList.length;index++) {
  97. inputList[index].checked = this.checked
  98. }
  99. }
  100. for(let index = 0;index < inputList.length;index++) {
  101. inputList[index].onclick = function() {
  102. let flag = true
  103. for(let i = 0;i < inputList.length;i++) {
  104. if (inputList[i].checked === false) {
  105. flag = false
  106. }
  107. }
  108. allBtn.checked = flag
  109. }
  110. }
  111. }
  112. </script>
  113. </body>
  114. </html>

第八节课 - 图6第八节课 - 图7

表单-Tab切换(排他性)

  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>Tab切换(排他性)</title>
  8. <style>
  9. button {
  10. margin: 10px;
  11. width: 100px;
  12. height: 40px;
  13. cursor: pointer;
  14. }
  15. .current {
  16. background-color: #f00;
  17. }
  18. </style>>
  19. </head>
  20. <body>
  21. <button>按钮1</button>
  22. <button>按钮2</button>
  23. <button>按钮3</button>
  24. <button>按钮4</button>
  25. <button>按钮5</button>
  26. <script>
  27. let btnList = document.getElementsByTagName('button')
  28. for(let i = 0;i < btnList.length;i++) {
  29. btnList[i].onmouseover = function() {
  30. for(let j = 0;j < btnList.length;j++) {
  31. btnList[j].className=''
  32. }
  33. this.className='current'
  34. }
  35. }
  36. for(let i = 0;i < btnList.length;i++) {
  37. btnList[i].onmouseout = function() {
  38. this.className=''
  39. }
  40. }
  41. </script>
  42. </body>
  43. </html>

第八节课 - 图8

二维码悬浮

  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. </head>
  9. <body>
  10. <!--下面这个IDimgelement是略缩图,鼠标悬停在它上面即出现原图-->
  11. <!--onmouseover方法中调用了mouseOverPic函数,传给它的参数是原图的URL-->
  12. <!--调用mouseOverPic之后,将原图显示区域设为可见-->
  13. <img id="img" src="me.jpg" width="50px" height="50px" onmouseover="mouseOverPic('me.jpg'); bigPic.style.visibility='visible'" onmouseout="mouseOutPic()">
  14. <!--页面初始化的时候,原图显示区域是隐藏的-->
  15. <div id="bigPic" style="visibility:hidden;">
  16. </div>
  17. <script language=JavaScript>
  18. function mouseOutPic() //当鼠标移出时,隐藏原图
  19. {
  20. if(window.event.toElement.id!="img") bigPic.style.visibility = "hidden";
  21. //如果鼠标不在img000上的话,将这个区域隐藏
  22. }
  23. function mouseOverPic(imgSrc) //当鼠标移上小图时,显示原图
  24. {
  25. var seeBig = document.getElementById("bigPic"); //得到将要显示原图区域的div element
  26. var newImg = document.createElement("img"); //新建一个img element,将原图的src赋给这个element
  27. newImg = "<img src=\'" + imgSrc + "\'/>"; //注意这里,是为整个element赋值,我在这个小问题上还花了挺多时间
  28. //alert(imgSrc);
  29. seeBig.innerHTML = newImg; //将原图赋给要显示的区域
  30. }
  31. </script>
  32. </body>
  33. </html>

第八节课 - 图9

图片遮罩层

  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. ul {
  10. padding: 0;
  11. }
  12. li {
  13. float: left;
  14. padding: 1em;
  15. list-style: none;
  16. }
  17. li a img {
  18. border: 0;
  19. }
  20. .clear {
  21. clear: both;
  22. }
  23. #image-gallery {
  24. width: 500px;
  25. overflow: auto;
  26. display: flex;
  27. background-color: silver;
  28. }
  29. #mask {
  30. position: absolute;
  31. top: 0;
  32. bottom: 0;
  33. left: 0;
  34. right: 0;
  35. background-color: rgba(0, 0, 0, 0.5);
  36. display: none;
  37. }
  38. .preview {
  39. width: 800px;
  40. height: 500px;
  41. position: absolute;
  42. top: 50%;
  43. left: 50%;
  44. margin-left: -400px;
  45. margin-top: -250px;
  46. }
  47. #image-gallery {
  48. widows: 500px;
  49. overflow: auto;
  50. display: flex;
  51. background-color: #b292ac;
  52. }
  53. #mask {
  54. position: absolute;
  55. top: 0;
  56. bottom: 0;
  57. left: 0;
  58. right: 0;
  59. background-color: rgba(0, 0, 0, 0.6);
  60. display: none;
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <h2>图片画廊</h2>
  66. <ul id = "image-gallery">
  67. <li>
  68. <a href="img/1.jpg" title="图片1">
  69. <img src="1.jpg" alt="图片1" width="100">
  70. </a>
  71. </li>
  72. <li>
  73. <a href="img/2.jpg" title="图片2">
  74. <img src="2.jpg" alt="图片2" width="100">
  75. </a>
  76. </li>
  77. <li>
  78. <a href="img/3.jpg" title="图片3">
  79. <img src="3.jpg" alt="图片3" width="100">
  80. </a>
  81. </li>
  82. <li>
  83. <a href="img/4.jpg" title="图片4">
  84. <img src="4.jpg" alt="图片4" width="100">
  85. </a>
  86. </li>
  87. <li>
  88. <a href="img/5.jpg" title="图片5">
  89. <img src="5.jpg" alt="图片5" width="100">
  90. </a>
  91. </li>
  92. </ul>
  93. <div class="clear"></div>
  94. <!--<img id = "image" src="150.png" />-->
  95. <p id="des">选择一个图片</p>
  96. <div id="mask">
  97. <img id="image" class="preview" />
  98. </div>
  99. <script>
  100. let ul = document.getElementById('image-gallery')
  101. let arr = ul.getElementsByTagName('a')
  102. let img = document.getElementById('image')
  103. let des = document.getElementById('des')
  104. let mask = document.getElementById('mask')
  105. for (let index = 0;index < arr.length;index++) {
  106. const element = arr[index];
  107. element.onclick = function() {
  108. ul.style.zIndex = -999
  109. mask.style.display = 'block'
  110. img.src = this.href
  111. return false
  112. }
  113. img.onclick = function() {
  114. mask.style.display='none'
  115. ul.style.zIndex = 999
  116. }
  117. }
  118. </script>
  119. </body>
  120. </html>

第八节课 - 图10第八节课 - 图11