剛接觸creatElement ,appendChild 等東東!呵呵!
寫了這個東西,可能在高手看來,非常的稚嫩!不過完全自己寫的!
還沒有完善,請大俠指導!!
看代碼
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <style> .test{border:1px solid #ccc;background-color:#eee;width:200px;height:60px;} </style> <script language="javascript"> <!-- function isIE(){ //ie? if (window.navigator.userAgent.indexOf("MSIE")>=1) return true; else return false; } if(!isIE()){ //firefox innerText define HTMLElement.prototype.__defineGetter__("innerText", function(){ return this.textContent; } ); HTMLElement.prototype.__defineSetter__("innerText", function(sText){ this.textContent=sText; } ); }function $(e){ return document.getElementById(e); } var arr=new Array(); function check(e){ var v=0; for(i=0;i<arr.length;i++) {if(arr[i]==e) v=1; else v=0;} if(v) return true; else return false; } function edit(e){ var x=$(e); var str=x.innerText; if (check(e)) { //alert("已經(jīng)存在在數(shù)組里!"); //alert(str); var b=$(e+"fa"); str=$(e+"faz").value; b.removeChild($(e+"faz")); x.innerText=str; }else{ //alert("沒有存在在數(shù)組里!"); a1=document.createElement("div"); a1.id=e+"fa"; a=document.createElement("INPUT"); a.name=e; a.id=e+"faz"; a.value=str; x.innerHTML=""; x.appendChild(a1); a1.appendChild(a); if(arr.length==0) arr[0]=e; else arr[arr.length]=e; //alert(a.name); } } --> </script> </HEAD> <BODY> <div class="test" id="test" onclick="edit('test')">可編輯的內(nèi)容</div> <div class="test" id="test1" onclick="edit('test1')">可編輯的內(nèi)容</div> </BODY> </HTML>最后解決瀏覽器問題的代碼
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> New Document </TITLE> <style> body{font-size:14px;} input{border:none;font-size:14px;} .show{border:1px solid #ccc;background-color:#eee;width:150px;height:20px;} .hide{border:1px solid #ccc;background-color:#eee;width:150px;height:20px;display:none;} </style> <script language="javascript"> <!-- function $(e){ return document.getElementById(e);//封裝獲取函數(shù) } function edit(e){ //parseInt $(e).style.display="none"; $(e+"fm").style.display="block"; $(e+"fm").childNodes[0].focus(); //div的子元素自動獲得焦點 } function hide(e){ //parseInt $(e).style.display="none"; $(e=e.replace("fm","")).style.display="block"; } --> </script> </HEAD> <BODY> 唉!!忙了一晚上,就這么解決了!!嘻嘻 呵呵
郁悶!<br> <font color=red><b>在firefox下 由于DOM有很小的差異,含有input的div中除標簽內(nèi)的空格外</b></font><br> <font color=red><b>不能有空格因為我們用的是childNodes[0]!否則需要點兩下!</b></font> <div> <div class="show" id="test" onclick="edit(this.id)">可編輯的內(nèi)容一</div> <div class="hide" id="testfm"><input type="text" name="nn" value="可編輯的內(nèi)容一" onBlur="hide(this.parentNode.id)" /></div> <div class="show" id="test1" onclick="edit(this.id)">可編輯的內(nèi)容二</div> <div class="hide" id="test1fm"><input type="text" name="nn" value="可編輯的內(nèi)容二" onBlur="hide(this.parentNode.id)" /></div> <div class="show" id="test2" onclick="edit(this.id)">可編輯的內(nèi)容三</div> <div class="hide" id="test2fm"> <input type="text" name="nn" value="可編輯的內(nèi)容三" onBlur="hide(this.parentNode.id)" > </div> <div class="show" id="test3" onclick="edit(this.id)">可編輯的內(nèi)容四</div> <div class="hide" id="test3fm"> <input type="text" name="nn" value="可編輯的內(nèi)容四" onBlur="hide(this.parentNode.id)" > </div> </div> </BODY> </HTML>
以上所述是小編給大家介紹的不用ajax實現(xiàn)點擊文字即可編輯的方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
新聞熱點
疑難解答