insertAdjacentHTML方法详解

添加HTML内容与文本内容以前用的是innerHTML与innerText方法,

最近发现还有insertAdjacentHTML和 insertAdjacentText方法,

这两个方法更灵活,可以在指定的地方插入html内容和文本内容。


insertAdjacentText方法与 insertAdjacentHTML方法类似,只不过只能插入纯文本,参数相同

方法名称:insertHtml(where,el,html)

参数介绍:

  where:插入位置。包括beforeBegin,beforeEnd,afterBegin,afterEnd。
  el:用于参照插入位置的html元素对象
  html:要插入的html代码


  insertAdjacentHTML 方法:在指定的地方插入html标签语句

  原型:insertAdajcentHTML(swhere,stext)  


  参数:

    swhere: 指定插入html标签语句的地方,

    text:要插入的内容



  有四种值可用:

    1.     beforeBegin: 插入到标签开始前

    2.     afterBegin:插入到标签开始标记之后

    3.     beforeEnd:插入到标签结束标记前

    4.     afterEnd:插入到标签结束标记后


  案例:

    viewSkeletonRender(info){
      this.$refs.fullCalendar.$el.querySelector('.fc-header-toolbar').insertAdjacentHTML('afterbegin', `
        <div>22222</div>
      `);
    }
  

 

posted on 2021-03-11 15:34  ㅤㅤㅤㅤㅤㅤ  阅读(210)  评论(0编辑  收藏  举报

导航