如图
当点击编辑时可以改变文本,点击取消则返回
代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
span{ margin-right:10px; }
.updata{ display:none};
</style>
</head>
<body>
<ul>
<li>
<div>
<span>白日依山尽</span><span>编辑</span>
</div>
<div class="updata">
<input type="text">
<span>保存</span>
<span>取消</span>
</div>
</li>
<li>
<div>
<span>黄河入海流</span><span>编辑</span>
</div>
<div class="updata">
<input type="text">
<span>保存</span>
<span>取消</span>
</div>
</li>
<li>
<div>
<span>欲穷千里目</span><span>编辑</span>
</div>
<div class="updata">
<input type="text">
<span>保存</span>
<span>取消</span>
</div>
</li>
<li>
<div>
<span>更上一层楼</span><span>编辑</span>
</div>
<div class="updata">
<input type="text">
<span>保存</span>
<span>取消</span>
</div>
</li>
</ul>
<script>
var ali = document.getElementsByTagName('li')
for(var i=0; i<ali.length;i++){
fn(ali[i])
}
function fn(lis){
var odiv = lis.getElementsByTagName('div')
var ospan = odiv[0].getElementsByTagName('span')
var uspan = odiv[1].getElementsByTagName('span')
var oinp = odiv[1].getElementsByTagName('input')[0]
ospan[1].onclick = function(){
odiv[0].style.display = 'none';
odiv[1].style.display = 'block';
oinp.value = ospan[0].innerHTML
}
uspan[0].onclick = function(){
dis()
ospan[0].innerHTML= oinp.value
}
uspan[1].onclick = function(){
dis()
}
function dis(){
odiv[0].style.display = 'block';
odiv[1].style.display = 'none';
}
}
</script>
</body>
</html>