Blogger Information
Blog 47
fans 3
comment 0
visits 38320
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
构造函数的原型和对象之间区别与联系、获取dom元素方法、dom元素增删改查常用操作
Original
580 people have browsed it

1. 构造函数的原型和对象之间的区别与联系

实例对象的原型指向它的构造函数原型
实例对象的原型从构造函数的原型继承成员(属性/方法)

  1. <script>
  2. // 声明一个构造函数
  3. function User(name, email) {
  4. this.name = name;
  5. this.email = email;
  6. }
  7. const user1 = new User("天蓬", "tp@php.cn");
  8. console.log(user1);
  9. user1.__proto__.salary = 888;
  10. console.log(user1.salary);
  11. // 实例的原型永远指向它的构造函数的原型
  12. // 返回true
  13. console.log(user1.__proto__ === User.prototype);
  14. // 需要被所有类实例共享的成员,应该写到构造函数的原型上
  15. User.prototype.nation = "中国";
  16. console.log(user1.nation);
  17. // 属性通常不应该共享的,它是区分不同对象的标签,方法更适合更像
  18. User.prototype.show = function () {
  19. return { name: this.name, email: this.email };
  20. };
  21. const user2 = new User("奥特曼", "atm@php.cn");
  22. console.log(user2.show());
  23. </script>

2. 获取dom元素方法

css选择器:
document.querySelectorALL()获取满足条件的所有元素
document.querySelector()获取满足条件的第一个元素

传统方法:
document.getElementById()
document.getElementsByTagName()
document.getElementsByName()
document.getElementsByClassName()

快捷方式:
html:document.documentElement
head:document.head
body:document.body
title:document.title
forms:document.forms document.forms.item(0)

  1. <ul id="list">
  2. <li class="item">test1</li>
  3. <li class="item">test2</li>
  4. <li class="item">test3</li>
  5. <li class="item">test4</li>
  6. <li class="item">test5</li>
  7. </ul>
  8. <form action="">
  9. <input type="text">
  10. </form>
  11. <script>
  12. // 使用css选择器是最直观的
  13. // 1. 获取满足条件的所有元素
  14. const lis = document.querySelectorAll("#list li");
  15. let larr = [...lis];
  16. console.log(larr);
  17. console.log("----------------------");
  18. let first = document.querySelectorAll("#list li:first-of-type");
  19. console.log(first);
  20. // 2. 获取满足条件的第一个元素
  21. first = document.querySelector("#list li");
  22. console.log(first);
  23. // 快捷方式,快速获取某一个或某一类元素
  24. // html
  25. // console.log(document.docume。ntElement);
  26. // head
  27. console.log(document.head);
  28. // body
  29. console.log(document.body);
  30. // title
  31. console.log(document.title);
  32. // forms
  33. console.log(document.forms);
  34. console.log(document.forms.item(0));
  35. </script>

3. dom元素增删改查常用操作

  • dom元素增加

  1. <ul id="list">
  2. <li>test1</li>
  3. <li>test2</li>
  4. <li>test3</li>
  5. <li>test4</li>
  6. <li>test5</li>
  7. </ul>
  8. <script>
  9. const ul = document.querySelector("#list");
  10. const li = document.createElement("li");
  11. let htmlStr = "<li style='color:blue'>test7</li>";
  12. // 开头新增一个
  13. ul.insertAdjacentHTML("afterbegin", htmlStr);
  14. </script>

  1. <ul id="list">
  2. <li>test1</li>
  3. <li>test2</li>
  4. <li>test3</li>
  5. <li>test4</li>
  6. <li>test5</li>
  7. </ul>
  8. <script>
  9. const ul = document.querySelector("#list");
  10. const li = document.createElement("li");
  11. let htmlStr = "<li style='color:blue'>test7</li>";
  12. //末尾新增一个
  13. ul.insertAdjacentHTML("beforeend", htmlStr);
  14. </script>

  1. <ul id="list">
  2. <li>test1</li>
  3. <li>test2</li>
  4. <li>test3</li>
  5. <li>test4</li>
  6. <li>test5</li>
  7. </ul>
  8. <script>
  9. const ul = document.querySelector("#list");
  10. const li = document.createElement("li");
  11. //新增多个
  12. const frag = new DocumentFragment();
  13. for (let i = 0; i < 5; i++) {
  14. const li = document.createElement("li");
  15. li.textContent = "Hello" + (i + 1);
  16. // 将生成的节点先临时挂载到文档片断中
  17. frag.appendChild(li);
  18. }
  19. ul.appendChild(frag);
  20. </script>
  • dom元素删除

  1. <ul id="list">
  2. <li>test1</li>
  3. <li>test2</li>
  4. <li>test3</li>
  5. <li>test4</li>
  6. <li>test5</li>
  7. </ul>
  8. <script>
  9. const ul = document.querySelector("#list");
  10. const li = document.createElement("li");
  11. //删除第三个
  12. ul.removeChild(document.querySelector("#list li:nth-of-type(3)"));
  13. </script>
  • dom元素修改

  1. <ul id="list">
  2. <li>test1</li>
  3. <li>test2</li>
  4. <li>test3</li>
  5. <li>test4</li>
  6. <li>test5</li>
  7. </ul>
  8. <script>
  9. const ul = document.querySelector("#list");
  10. const li = document.createElement("li");
  11. //修改第三个
  12. const replaceItem = document.createElement("li");
  13. replaceItem.innerHTML = "item3-3";
  14. document.querySelector("#list li:nth-of-type(3)").replaceWith(replaceItem);
  15. </script>
  • dom元素查询

  1. <ul id="list">
  2. <li>test1</li>
  3. <li>test2</li>
  4. <li>test3</li>
  5. <li>test4</li>
  6. <li>test5</li>
  7. </ul>
  8. <script>
  9. const ul = document.querySelector("#list");
  10. const li = document.createElement("li");
  11. // 获取所有子元素
  12. console.log(ul.children);
  13. // 获取子元素数量
  14. // console.log(ul.children.length);
  15. console.log(ul.childElementCount);
  16. // 第一个子元素
  17. console.log(ul.firstElementChild);
  18. // 最后一个
  19. console.log(ul.lastElementChild);
  20. // 父节点
  21. console.log(ul.lastElementChild.parentElement);
  22. // 获取前一个兄弟
  23. const ba = document.querySelector("#list li:nth-of-type(3)");
  24. ba.style.background = "violet";
  25. // 获取前一个兄弟:tset1
  26. console.log(ba.previousElementSibling.innerHTML);
  27. // 后一个兄弟:test3
  28. console.log(ba.nextElementSibling.innerHTML);
  29. </script>
Correcting teacher:天蓬老师天蓬老师

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