兼容FF/IE的insertAdjacentHTML方法

  1. function insertHtml(where, el, html){   
  2.            where = where.toLowerCase();   
  3.           if(el.insertAdjacentHTML){   
  4.               switch(where){   
  5.                   case "beforebegin":   
  6.                        el.insertAdjacentHTML('BeforeBegin', html);   
  7.                      return el.previousSibling;   
  8.                   case "afterbegin":   
  9.                       el.insertAdjacentHTML('AfterBegin', html);   
  10.                      return el.firstChild;   
  11.                   case "beforeend":   
  12.                       el.insertAdjacentHTML('BeforeEnd', html);   
  13.                      return el.lastChild;   
  14.                   case "afterend":   
  15.                       el.insertAdjacentHTML('AfterEnd', html);   
  16.                      return el.nextSibling;   
  17.               }   
  18.                throw 'Illegal insertion point -> "' + where + '"';   
  19.          }   
  20.      var range = el.ownerDocument.createRange();   
  21.            var frag;   
  22.           switch(where){   
  23.                 case "beforebegin":   
  24.                    range.setStartBefore(el);   
  25.                    frag = range.createContextualFragment(html);   
  26.                   el.parentNode.insertBefore(frag, el);   
  27.                    return el.previousSibling;   
  28.                 case "afterbegin":   
  29.                    if(el.firstChild){   
  30.                        range.setStartBefore(el.firstChild);   
  31.                        frag = range.createContextualFragment(html);   
  32.                       el.insertBefore(frag, el.firstChild);   
  33.                        return el.firstChild;   
  34.                    }else{   
  35.                       el.innerHTML = html;   
  36.                       return el.firstChild;   
  37.                    }   
  38.               case "beforeend":   
  39.                    if(el.lastChild){   
  40.                        range.setStartAfter(el.lastChild);   
  41.                        frag = range.createContextualFragment(html);   
  42.                        el.appendChild(frag);   
  43.                        return el.lastChild;   
  44.                    }else{   
  45.                        el.innerHTML = html;   
  46.                        return el.lastChild;   
  47.                    }   
  48.                case "afterend":   
  49.                    range.setStartAfter(el);   
  50.                    frag = range.createContextualFragment(html);   
  51.                    el.parentNode.insertBefore(frag, el.nextSibling);   
  52.                   return el.nextSibling;   
  53.                }   
  54.                throw 'Illegal insertion point -> "' + where + '"';   
  55.        }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值