让firefox支持outerHTML的代码,外围标签怎么改为小写?外面包裹的标签是大写,其他的都是小写,请高手帮忙改一下,让全部标签都为小写。
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
// 让 ff支持 outerHTML
function enable_outerHTML(){
if(typeof(HTMLElement)!="undefined" && !window.opera) {
    HTMLElement.prototype.__defineGetter__("outerHTML",function()
    {
var a=this.attributes, str="<"+this.tagName, i=0;for(;i<a.length;i++)
if(a[i].specified)
    str+=" "+a[i].name+'="'+a[i].value+'"';
if(!this.canHaveChildren)
    return str+" />";
return str+">"+this.innerHTML+"</"+this.tagName+">";
    });
    HTMLElement.prototype.__defineSetter__("outerHTML",function(s)
    {
var r = this.ownerDocument.createRange();
r.setStartBefore(this);
var df = r.createContextualFragment(s);
this.parentNode.replaceChild(df, this);
return s;
    });
    HTMLElement.prototype.__defineGetter__("canHaveChildren",function()
    {
return !/^(area|base|basefont|col|frame|hr|img|br|input|isindex|link|meta|param)$/.test(this.tagName.toLowerCase());
    });
}
}
$(function(){
enable_outerHTML();
});

// 显示outerHTML
function show_html(){
alert($(".selected")[0].outerHTML);
}
</script>
<div class="selected">
<div>hello world</div>
</div>
<button onclick="show_html()">show_html</button>

解决方案 »

  1.   

    HTMLElement.prototype.__defineGetter__("outerHTML",function()
                {
                var a=this.attributes, str="<"+this.tagName, i=0;for(;i<a.length;i++)
                if(a[i].specified)
                    str+=" "+a[i].name+'="'+a[i].value+'"';
                if(!this.canHaveChildren)
                    return str+" />";
                return str+">"+this.innerHTML+"</"+this.tagName+">";
                });上面2个this.tagName改成this.tagName.toLowerCase()
      

  2.   

    谢谢高手sohighthesky,对纯js代码有点感冒,只会jquery。
    帮大忙了!