插入元素涉及的函数有两个:

一、append():在选择集末尾插入元素

  假设有三个段落元素

<p>Apple</p>
<p>Pear</p>
<p>Banana</p>
body.append("p")
    .text("append p element");

  在 body 的末尾添加一个 p 元素,结果为:

Apple
Pear
Banana
append p element

 

 

 

 

 

二、insert():在选择集前面插入元素

   在 body 中 id 为 myid 的元素前添加一个段落元素。

<p>Apple</p>
<p id="myid">Pear</p>
<p>Banana</p>
body.insert("p","#myid")
  .text("insert p element");

  已经指定了 Pear 段落的 id 为 myid,因此结果如下。

Apple
insert p element
Pear
Banana

 

 

 

 

 三、删除元素

  删除一个元素时,对于选择的元素,使用 remove 即可,例如:

var p = body.select("#myid");
p.remove();

 

相关文章:

  • 2021-04-23
  • 2022-12-23
  • 2021-09-19
  • 2021-09-20
  • 2021-12-25
  • 2022-12-23
  • 2021-08-26
猜你喜欢
  • 2022-12-23
  • 2022-01-17
  • 2021-12-04
  • 2022-12-23
  • 2021-12-26
  • 2021-12-15
  • 2021-09-17
相关资源
相似解决方案