Blogger Information
Blog 47
fans 0
comment 3
visits 45066
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
元素对象的insertAdjacentElement()方法
江流
Original
3791 people have browsed it

使用就是给Html元素进行添加操作,insertAdjacentElement();方法是个不错的选择。其基本结构为:
对象.insertAdjacentElement(参数1,参数2)

  • 对象:是直已经在页面中存在的HTML元素,以此为位置坐标点插入新对象;
  • 参数1:是插入操作的位置,有4个值
    • beforeBegin:对象的开始标签之前
    • afterBegin:对象的开始标签之后
    • beforeEnd:对象的结束标签之前
    • afterEnd:对象的结束标签之后
  • 参数2:已经创建的待插入对象。

一个简单人实例:

  • html
    ```html
  1. - css
  2. ```css

操作前的效果:

  • javascritp
    1. // 获取插入结点,并对结点进行样式设计
    2. const insert = document.querySelector(".insert");
    3. insert.style.backgroundColor = "red";
    4. insert.style.color = "blue";
    5. insert.style.fontSize = "20px";
    6. insert.style.padding = "20px";


插入操作

  1. // beforeBegin:对象的开始标签之前
  2. const bBegin = document.createElement("div");
  3. bBegin.textContent = "BeforBegin";
  4. insert.insertAdjacentElement("beforeBegin", bBegin);
  5. // - afterBegin:对象的开始标签之后
  6. const aBegin = document.createElement("div");
  7. aBegin.textContent = "AfterBegin";
  8. insert.insertAdjacentElement("afterBegin", aBegin);
  9. // - beforeEnd:对象的结束标签之前
  10. const bEnd = document.createElement("div");
  11. bEnd.textContent = "BeforEnd";
  12. insert.insertAdjacentElement("beforeEnd", bEnd);
  13. // - afterEnd:对象的结束标签之后
  14. const aEnd = document.createElement("div");
  15. aEnd.textContent = "AfterEnd";
  16. insert.insertAdjacentElement("AfterEnd", aEnd);

效果

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