Blogger Information
Blog 28
fans 0
comment 0
visits 15879
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
07-14作业 JSON 对象转换字符串 选项卡 懒加载
︷肉園耔︷
Original
483 people have browsed it

— 代码区 JSON 对象转换字符串

  1. let jsonStr=`
  2. [
  3. {"id":10,"name":"admin","email":"admin@php.cn"},
  4. {"id":11,"name":"peter","email":"peter@php.cn"},
  5. {"id":12,"name":"zhu","email":"zhu@php.cn"}
  6. ]
  7. `;
  8. // console.log(JSON.parse(jsonStr));
  9. //返回一个"对象数组"
  10. let users= JSON.parse(jsonStr);
  11. const table=document.createElement("table");
  12. table.border="1";
  13. table.cellPadding="5";
  14. table.cellSpacing="0";
  15. users.forEach(user=>{
  16. let tr=
  17. ` <tr>
  18. <td> ${user.id}</td>
  19. <td> ${user.name}</td>
  20. <td> ${user.email}</td>
  21. </tr>
  22. `;
  23. table.insertAdjacentHTML("beforeEnd",tr);
  24. table.insertAdjacentHTml
  25. });
  26. table.insertAdjacentHTML('afterBegin','<caption>用户表<caption>');
  27. document.body.insertAdjacentElement("afterbegin",table);
  28. //2.json在其他语言中使用
  29. //想一下,JS对象能不能直接被php处理》
  30. let book={
  31. id:"ISBN-123456",
  32. name:"JacaScript从入门到镜头",
  33. price:99,
  34. };
  35. let str=JSON.stringify(book);
  36. console.log(str);
  37. //{"id":"ISBN-123456","name":"JacaScript从入门到镜头","price":99}
  38. console.log(JSON.parse(['{"id":"ISBN-123456","name":"JacaScript从入门到镜头","price":99}']));
  39. let user={
  40. name:"msc",
  41. sex:"男",
  42. age:33,
  43. };
  44. let str1=JSON.stringify(user);
  45. console.log(str1);
  46. //转换字符串 {"name":"msc","sex":"男","age":33}
  47. console.log(JSON.parse(['{"name":"msc","sex":"男","age":33}'
  48. ]))

— 选项卡

  1. <div class="tabs">
  2. <ul class="tab">
  3. <li class="active" data-index="1">房产</li>
  4. <li data-index="2">科技</li>
  5. <li data-index="3">军事</li>
  6. </ul>
  7. <ul data-index="1" class="item active">
  8. <li><a href="">贵阳学区实现多校划片 </a></li>
  9. <li><a href="">学区实现多校划片</a></li>
  10. <li><a href="">学区实现多校划片</a></li>
  11. <li><a href="">学区实现多校划片</a></li>
  12. <li><a href="">学区实现多校划片</a></li>
  13. </ul>
  14. <ul data-index="2" class="item">
  15. <li><a href="">B站就服务器故障致歉 </a></li>
  16. <li><a href="">B站就服务器故障致歉</a></li>
  17. <li><a href="">B站就服务器故障致歉</a></li>
  18. <li><a href="">B站就服务器故障致歉</a></li>
  19. <li><a href="">B站就服务器故障致歉</a></li>
  20. </ul>
  21. <ul data-index="3" class="item">
  22. <li><a href="">印媒渲染阿斯蒂芬玩儿 </a></li>
  23. <li><a href="">印媒渲染阿斯蒂芬玩儿 </a></li>
  24. <li><a href="">印媒渲染阿斯蒂芬玩儿 </a></li>
  25. <li><a href="">印媒渲染阿斯蒂芬玩儿 </a></li>
  26. <li><a href="">印媒渲染阿斯蒂芬玩儿 </a></li>
  27. </ul>
  28. </div>
  29. <script>
  30. /* const res=[3,4,1,3,6,8,10,33,12,24].filter(function(item){
  31. return item > 5;
  32. })
  33. console.log(res);*/
  34. const tab = document.querySelector(".tab");
  35. const items=document.querySelectorAll(".item");
  36. tab.onclick= ev=>{
  37. //console.log(ev.currentTarget);
  38. // console.log(ev.target);
  39. //console.log([...tab.children]);
  40. [...tab.children].forEach(item=>item.classList.remove("active"));
  41. ev.target.classList.add("active");
  42. //根据导航的data-index来全都应该确认哪一个列表设置激活状态
  43. items.forEach(item=>item.classList.remove("active"));
  44. //console.log(items);
  45. const res=[...items]
  46. .filter(function(item){
  47. return item.dataset.index === ev.target.dataset.index;
  48. })
  49. .shift();
  50. console.log(res);
  51. };
  52. </script>

— 懒加载

  1. <script>
  2. /*const box =document.querySelector(".box");
  3. //box.getBoundingClientRect() 元素相对于可视区的位置
  4. const rect=box.getBoundingClientRect();
  5. //窗口中的可视区高度
  6. console.log(rect);
  7. //窗口的可视区高度
  8. console.log(window.innerHeight);*/
  9. const img=document.images[0];
  10. window.onscroll=()=>{
  11. // 获取图片位置小于窗口高度显示图片
  12. console.log(img.getBoundingClientRect().top);
  13. if(img.getBoundingClientRect().top<window.innerHeight){
  14. img.src= img.dataset.src;
  15. }
  16. }
  17. </script>
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