JavaScript 就地編輯HTML節點實現代碼_javascript技巧

來源:互聯網
上載者:User
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>點擊編輯當前內容</title> <script type="text/javascript"> 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元素進行就地編輯 } </script> </head> <body> <h3>Edit Demo</h3> <p id="p">雲棲社區 www.jb51.net</p> <p>Copyright:Super sha.</p> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.