学吧就 - 博客园
想知道更多兼容的好东西 访问我的网站吧 boahy.com 呵呵 打个广告 www.boahy.com
insertAdjacentHTML
这个东西我还没研究明白 真是不好整啊
- function insertHtml(where, el, html){
- where = where.toLowerCase();
- if(el.insertAdjacentHTML){
- switch(where){
- case "beforebegin":
- el.insertAdjacentHTML('BeforeBegin', html);
- return el.previousSibling;
- case "afterbegin":
- el.insertAdjacentHTML('AfterBegin', html);
- return el.firstChild;
- case "beforeend":
- el.insertAdjacentHTML('BeforeEnd', html);
- return el.lastChild;
- case "afterend":
- el.insertAdjacentHTML('AfterEnd', html);
- return el.nextSibling;
- }
- throw 'Illegal insertion point -> "' + where + '"';
- }
- var range = el.ownerDocument.createRange();
- var frag;
- switch(where){
- case "beforebegin":
- range.setStartBefore(el);
- frag = range.createContextualFragment(html);
- el.parentNode.insertBefore(frag, el);
- return el.previousSibling;
- case "afterbegin":
- if(el.firstChild){
- range.setStartBefore(el.firstChild);
- frag = range.createContextualFragment(html);
- el.insertBefore(frag, el.firstChild);
- return el.firstChild;
- }else{
- el.innerHTML = html;
- return el.firstChild;
- }
- case "beforeend":
- if(el.lastChild){
- range.setStartAfter(el.lastChild);
- frag = range.createContextualFragment(html);
- el.appendChild(frag);
- return el.lastChild;
- }else{
- el.innerHTML = html;
- return el.lastChild;
- }
- case "afterend":
- range.setStartAfter(el);
- frag = range.createContextualFragment(html);
- el.parentNode.insertBefore(frag, el.nextSibling);
- return el.nextSibling;
- }
- throw 'Illegal insertion point -> "' + where + '"';
- }
慢慢研究