íì¬ ì½í ì¸ ë¥¼ í¸ì§íë ¤ë©´ í´ë¦ ë°ëª¨ í¸ì§ ì¤í¬ë¦½í¸ í www.jb51.net ì ìê¶:ìí¼ì¤. [Ctrl A 모ë ì í ì°¸ê³ : ì¸ë¶ J를 ëì í´ì¼ íë ê²½ì° ì¤ííë ¤ë©´ ìë¡ ê³ ì³ì¼ í©ëë¤ ] var EditField={ configure:function(id){ this.id=id; this.createElements(); //å¨æçæç¼è¾è¾å ¥æ¡ this.toScan(); //åå§åå¨æçæçè¾å ¥æ¡åæé®ãå¾ ç¼è¾çDOMå ç´ çdisplayå±æ§ this.addEvents(); //ç»ç¸å ³çDOMå ç´ æ·»å äºä»¶çå¬å¨ }, events:function(elem,type,fn){ //ç¨äºæ·»å äºä»¶çéç¨å½æ° if(elem.attachEvent){ elem.attachEvent("on"+type,fn); }else if(elem.addEventListener){ elem.addEventListener(type,fn,false); }else{ elem["on"+type]=fn; } return elem; }, addEvents:function(){ //æ·»å äºä»¶ var that=this; this.events(this.btnSubmit,"click",function(){ that.save(); }); this.events(this.btnCancel,"click",function(){ that.cancel(); }); this.events(document.getElementById(this.id),"click",function(){ that.toEdit(); }); }, insertAfter:function(newNode,referenceNode){ //å°å¨æçæçè¾å ¥æ¡åæé®æå ¥å°å¾ ç¼è¾å ç´ çåé¢ if (referenceNode.nextSibling) { referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling); }else{ var p=referenceNode.parentNode || document.body; p.appendChild(newNode); } }, createElements:function(){ //å¨æçæè¾å ¥æ¡åæé® this.divContainer=document.createElement("div"); //this.parentElement.appendChild(this.divContainer); this.insertAfter(this.divContainer,document.getElementById(this.id)); this.input=document.createElement("input"); this.input.type="text"; this.input.value=document.getElementById(this.id).innerHTML; //åå§åå¼ this.divContainer.appendChild(this.input); this.btnSubmit=document.createElement("input"); this.btnSubmit.value="Submit"; this.btnSubmit.type="button"; this.divContainer.appendChild(this.btnSubmit); this.btnCancel=document.createElement("input"); this.btnCancel.type="button"; this.btnCancel.value="Cancel"; this.divContainer.appendChild(this.btnCancel); }, toEdit:function(){ //转æ¢æç¼è¾ç¶æ this.divContainer.style.display="block"; document.getElementById(this.id).style.display="none"; this.setValue(); }, toScan:function(){ //转æ¢ææµè§ç¶æ document.getElementById(this.id).style.display="block"; this.divContainer.style.display="none"; }, getValue:function(){ //è·åè¾å ¥æ¡çå¼ return this.input.value; }, setValue:function(){ //设置è¾å ¥æ¡çå¼ this.input.value=document.getElementById(this.id).innerHTML; }, save:function(){ //ä¿åç¼è¾ç»æ document.getElementById(this.id).innerHTML=this.getValue(); this.toScan(); }, cancel:function(){ //åæ¶å½åçç¼è¾ this.toScan(); } }; onload=function(){ EditField.configure("p"); //è°ç¨configureå½æ°ï¼ç¡®å®é£ä¸ªDOMå ç´ è¿è¡å°±å°ç¼è¾ }