Blogger Information
Blog 28
fans 0
comment 0
visits 15758
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
07-15作业:简单的相册管理器
︷肉園耔︷
Original
457 people have browsed it
  1. 图例

  1. HTML+JS
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>Document</title>
  9. </head>
  10. <body>
  11. <div class="container">
  12. <ul>
  13. <li><img src=" " alt=""></li>
  14. </ul>
  15. </div>
  16. <script>
  17. //所以图片放在数组中
  18. const imgs = ["/img/1.jpg",
  19. "/img /2.jpg",
  20. "/img/3.jpg",
  21. "/img/4.jpg",
  22. "/img/5.jpg",];
  23. //动态生成图片
  24. const ul =document.querySelector('ul');
  25. //当页面加载完成时将所有的图片显示出来
  26. window.onload =showImgs;
  27. //显示所有图片
  28. function showImgs(){
  29. //使用数组迭代器来生出当前的图片元素
  30. let res= imgs.reduce((prev,curr) => {
  31. let tpl=`
  32. <li>
  33. <img src="${curr}" alt="" />
  34. <div class="btns">
  35. <button onclick="prev(this.parentNode.parentNode)">向前</button>
  36. <button onclick="prev(this.parentNode.parentNode)">向后</button>
  37. <button onclick="del(this.parentNode.parentNode)">删除</button>
  38. </div>
  39. </li>
  40. `;
  41. //最终结果是通过prev返回的,因为返回的是一个字符串
  42. return prev + tpl;
  43. },"");
  44. console.log(res);
  45. //插入到页面中
  46. ul.insertAdjacentHTML('beforeEnd',res);
  47. }
  48. //删除
  49. function del(ele){
  50. //ele:要被删除的元素参数
  51. if(confirm("是否删除")){
  52. ele.remove();
  53. }
  54. //三元
  55. return confirm('是否删除?')? ele.remove():false;
  56. }
  57. //向前
  58. function prve(ele){
  59. //判断有没有前一个节点
  60. if(ele.previousElementSibling == null){
  61. alert("已经是第一张了~~");
  62. return false;
  63. }
  64. let prevNode = ele.previousElementSibling;
  65. //parentNode.insertBefore(插入元素,插入位置)
  66. ul.insertBefore(ele,prevNode);
  67. }
  68. //向后
  69. function next(ele){
  70. //判断有没有下一个节点
  71. if(ele.nextElementSibling == null){
  72. alert("已经是最后一张了~~");
  73. return false;
  74. }
  75. let nextNode = ele.nextElementSibling;
  76. //parentNode.insertBefore(插入元素,插入位置)
  77. ul.insertBefore(nextNode,ele);
  78. }
  79. </script>
  80. </body>
  81. </html>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
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