ã¯ãªãã¯ãã¦ç¾å¨ã®ã³ã³ãã³ããç·¨éãã¾ã ãã¢ãç·¨é ã¹ã¯ãªãã ãã¼ã www.jb51.net èä½æ¨©:ã¹ã¼ãã¼ã·ã£ [Ctrl A ãã¹ã¦é¸æ 注: å¤é¨ Js ãå°å ¥ããå¿ è¦ãããå ´åã¯ã ãå®è¡ããããã«æ´æ°ããå¿ è¦ãããã¾ã] 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å ç´ è¿è¡å°±å°ç¼è¾ }