日期:2014-05-16 浏览次数:20440 次
<div> <div onclick="txtReplick">第一条测试</div> <div onclick="txtReplick">第二条测试</div> <div onclick="txtReplick">第三条测试</div> </div>
<!DOCTYPE HTML> <html> <head> <meta charset="gb2312" /> <title></title> <link rel="stylesheet" href="http://yui.yahooapis.com/2.9.0/build/reset/reset-min.css" /> <style> body {font-size:12px;} </style> </head> <body> <div onclick="txtReplick">第一条测试</div> <div onclick="txtReplick">第二条测试</div> <div onclick="txtReplick">第三条测试</div> <script> function $(el){ return typeof el == 'string' ? document.getElementById(el) : el; } function $t(name, cot){ cot = cot || document; return cot.getElementsByTagName(name); } var divs = $t('div'); for(var i=0, len=divs.length; i<len; i++){ divs[i].onclick = function(){ if( $t('input', this).length ) return ; var c = document.createElement('div'); c.innerHTML = '<input value="'+this.innerHTML+'" /><button>修改</button>'; this.appendChild(c); var that = this; $t('button', this)[0].onclick = function(e){ that.innerHTML = $t('input', that)[0].value; e = e || window.event; if (e.stopPropagation) { e.stopPropagation(); } else { e.cancelBubble = true; } } } } </script> </body> </html>
------解决方案--------------------
<div> <div onclick="txtReplick(this)" flag="off">第一条测试</div> <div onclick="txtReplick(this)" flag="off">第二条测试</div> <div onclick="txtReplick(this)" flag="off">第三条测试</div> </div> <script type="text/javascript"> function txtReplick(el){ var bool=el.getAttribute('flag'); if(bool=='off'){ el.setAttribute('flag','on') var tf=document.createElement('input'); tf.type='text'; tf.value=el.innerHTML; el.appendChild(tf); var btn=document.createElement('input'); btn.type='button'; btn.value='更改'; el.appendChild(btn); btn.onclick=function(event){ stopBubble(event); el.innerHTML=tf.value; el.setAttribute('flag','off'); el=null; } } } function stopBubble(e){ if(e && e.stopPropagation){ e.stopPropagation(); //w3c }else window.event.cancelBubble=true; //IE } </script>