7-1实现输入input框的数据以li的形式加到页面中

indexOf 获取数组的下标

  1. <script>
  2. var arr=["html","css"];
  3. //indexOf 获取数组的下标
  4. console.log(arr.indexOf("html"));
  5. </script>

实现

  1. <input type="text" name="" id="input">
  2. <ul id="app"></ul>
  3. <script>
  4. var arr = [];
  5. var input = document.getElementById("input");
  6. var app = document.getElementById("app");
  7. input.onkeydown = function (event) {
  8. if (event.keyCode == 13) {
  9. if (arr.indexOf(this.value) == -1 && this.value!="") {
  10. arr.push(this.value);
  11. var li = document.createElement("li");
  12. li.innerHTML = this.value;
  13. app.append(li);
  14. }
  15. console.log(arr)
  16. }
  17. }
  18. </script>

7-2实现从一个数组中获取想要的任意元素

  1. <script>
  2. var obj=[
  3. {name:"ming",age:18,skill:"js"},
  4. {name:"chen",age:18,skill:"vue"},
  5. {name:"zheng",age:18,skill:"jquery"},
  6. ]
  7. var arr=[];
  8. for(var i=0;i<obj.length;i++){
  9. // var name=obj[i].name;
  10. // var skill=obj[i].skill;
  11. var{name,skill}=obj[i];//解构
  12. //键和值(key:value)相同的时候可以只写一个
  13. arr.push({
  14. name,//name:name,
  15. skill//skill:skill
  16. })
  17. }
  18. console.log(arr)
  19. </script>

7-3实现界面切换

  1. <style>
  2. .parent {
  3. position: relative;
  4. }
  5. .parent>div{
  6. width: 200px;
  7. height: 200px;
  8. position:absolute;
  9. }
  10. .html{
  11. background-color: red;
  12. z-index: 100;
  13. }
  14. .css{
  15. background-color: yellow;
  16. }
  17. .current{
  18. color:blue;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <ul>
  24. <li class="current">html</li>
  25. <li>css</li>
  26. </ul>
  27. <div class="parent">
  28. <div class="html">
  29. html
  30. </div>
  31. <div class="css">
  32. css
  33. </div>
  34. </div>
  35. <script>
  36. var lis = document.getElementsByTagName("li");
  37. var divs=document.querySelectorAll(".parent div")
  38. for (let i = 0; i < lis.length; i++) {
  39. lis[i].index = i;//获取元素下标
  40. lis[i].onclick = function () {
  41. //1.遍历让所有的li移除class="current"
  42. for (let i = 0; i < lis.length; i++) {
  43. lis[i].classList.remove("current");
  44. }
  45. //2.让当前的元素添加class="current"
  46. this.classList.add("current");
  47. console.log(this.index)
  48. //3让所有的div隐藏
  49. for(let i=0;i<divs.length;i++){
  50. divs[i].style.display="none"
  51. }
  52. //4.让对应的下标的div显示
  53. divs[this.index].style.display="block";
  54. }
  55. }
  56. </script>