EXT2中一段兼容FF的insertAdjacentHTML方法

近日在搞一个关于多文件上传的组件,在IE内已实现了,并能成功动态的添加上传的附件.可是到FF下一测发觉没有任何反应 .原来我使用的是insertAdjacentHTML.方法不行.想起EXT2中有一段关于兼容FF的insertAdjacentHTML方法.就去COPY过来了.

 

<script type="text/javascript">
  
<!--
    
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 + '"';
    }


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

 

一切OK.

最近在看EXT方面的资源.收获不少.各位有兴趣不妨多看看.

posted @ 2007-12-21 08:59  greystar  阅读(191)  评论(0编辑  收藏  举报