学吧就 - 博客园

想知道更多兼容的好东西   访问我的网站吧  boahy.com  呵呵  打个广告 www.boahy.com

 

 

insertAdjacentHTML

 这个东西我还没研究明白  真是不好整啊

 

 

 

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

 

 

 

 

 

慢慢研究

posted @ 2010-09-14 20:08  boahy  阅读(184)  评论(0编辑  收藏  举报