六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 28|回复: 0

一个很有用的Prototype扩展---outerHTML方法

[复制链接]

升级  30%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
15
 楼主| 发表于 2013-2-7 15:41:24 | 显示全部楼层 |阅读模式
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());     }); }
使用方法:
<div id="test">   <input type="text" name="text" />   <p>this is a sentence</p></div><script>var out = document.getElementById("test").outerHTML;</script>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表