• Element.insertAdjacentHTML()方法由IE引入,并在HTML5中标准化,它将任意的HTML标记字符串插入到指定的元素“相邻”的位置。

  insertAdjacentHTML()有两个参数,第一个参数为位置属性,第二个参数为待插入的标记字符串。且第一个参数为具有以下值之一的字符串:“beforebegin”、"afterbegin"、"beforeend"、"afterend",这些值插入点为:

  (beforebegin) <div id="target"> (afterbegin) This is the element content  (beforeend) </div> (afterend)

  • 插入节点的为Node的方法appendChild()insertBefore()

  1. appendChild()方法是在需要插入的Element节点上调用的,它插入指定的节点使用其成为那个节点的最后一个节点。
  2. insertBefore()方法是新节点的父节点上调用并接受两个参数。第一个参数是待插入的节点,第二个参数是已存在的节点,新节点将插入到该节点的前面。如果第二个参数为null,其行为和appendChild()一样。
  • DocumentFragment是一种特殊的Node,它作为其他节点的一个临时的容器。  

  注意documentFrament的parentNode总是为null。如果给appendChild()、insertBefore()、replaceChild()传递一个DocumentFragment,其实是将该文档片段的所有子节点插入到文档中,而非片段本身。

为了使insertAdjacentHTML()方法兼容所有浏览器,并给它定义了一组更符合逻辑的名字,使用insertBefore()实现Insert.before(),Insert.after(),Insert.atAfter(),Insert.atEnd()方法。代码如下:

var Insert=(function(){
//如果元素有原生的insertAdjacentHTML,
//在4个函数名了的HTML插入函数中直接使用
if(document.createElement("div").insertAdjacentHTML){
return {
before:function(e,h){e.insertAdjacentHTML("beforebegin",h);},
after:function(e,h){e.insertAdjacentHTML("afterend",h);},
atStart:function(e,h){e.insertAdjacentHTML("beforebegin",h);},
atEnd:function(e,h){e.insertAdjacentHTML("afterend",h);}
};
}
//浏览不支持insertAdjacentHTML则通过insertBefor来实现
function fragment(html){
var elt=document.createElement("div"); //创建空文档
var frag=document.createDocumentFragment(); //创建空文档片段
elt.innerHTML=html; //设置文档内容
while(elt.firstChild) //移动所有的节点
frag.appendChild(elt.firstChild);
return frag;
} var Insert={
before:function(elt,html){
elt.parentNode.insertBefore(frament(html),elt);
},
after:function(elt,html){
elt.parentNode.insertBefore(fragment(html),elt.nextSibling);
},
atStart:function(elt,html){
elt.insertBefore(fragment(html),elt.firstChild);
},
atEnd:function(elt,html){
elt.insertBefore(fragment(html));
//insertBefore第二参数为null时,其行为类似于appendHTML(),它将节点插入到最后。
}
} Element.prototype.insertAdjacentHTML=function(pos,html){
switch(pos.toLowerCase()){
case "beforebegin":return Insert.before(this,html);
case "afterend":return Insert.after(this,html);
case "afterbegin":return Insert.atStart(this,html);
case "beforeend":return Insert.atEnd(this,html);
}
}
return Insert;
}());

  引自《JavaScript权威指南》 15.6

最新文章

  1. Http组件的介绍
  2. javascript实现原生ajax的方法
  3. stack, deque 和 queue的对比
  4. 使用163CentOS镜像
  5. 309. Best Time to Buy and Sell Stock with Cooldown
  6. ASP.NET最常用的页面生命周期事件
  7. 创建并使用静态库(ar 命令)
  8. python线程Example
  9. CSU 1811 Tree Intersection
  10. could not resolve property问题(ssh框架)
  11. IIS 部署WCF服务注意事项
  12. C#监控类属性的更改(大花猫动了哪些小玩具)
  13. 深度拾遗(01) - 梯度爆炸/梯度消失/Batch Normal
  14. Axure RP初学
  15. vue.js+socket.io+express+mongodb打造在线聊天
  16. Storm学习笔记 - Storm初识
  17. spring4笔记----web.xml中2.4以上版本Listener的配置
  18. BZOJ1022[SHOI2008]小约翰的游戏——anti-SG(反尼姆博弈)
  19. xhr文件类型说明
  20. 指令-Directive

热门文章

  1. MementoPattern(备忘录模式)
  2. Android:Activity+Fragment及它们之间的数据交换.
  3. atitit.管理学三大定律:彼得原理、墨菲定律、帕金森定律
  4. 简单Linux命令学习笔记
  5. LeetCode All in One 题目讲解汇总(持续更新中...)
  6. 让ASP.NET接受有“潜在危险”的提交
  7. js中几种实用的跨域方法原理详解(转)
  8. 微信小程序首次官方分享的纪要
  9. JavaScript面向对象
  10. MongoDB学习笔记~数据模型属性为集合时应该为它初始化