【JS】纯JS实现div中的文字双击可编辑

<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"><title>JS edit</title></head><body><div>
</div>
<div onClick="edit(this)">nanjing</div>
<script>

var titleEditing = false
function edit(element) {
console.log(titleEditing)
if(titleEditing){
return
}
titleEditing = true
let old = element.innerHTML
element.innerHTML=""
var newObj=document.createElement("textarea")
newObj.className="textArea"
newObj.innerHTML=old
element.append(newObj)
newObj.focus()
newObj.onblur=function(){
element.innerHTML=this.value?this.value:old
titleEditing = false
}
}
</script>
</body>
</html>

 

posted @ 2019-09-12 09:03  RedGuardian  阅读(1372)  评论(0编辑  收藏  举报