Update after change
实现功能
Now here is the code:
<html>
<head>
<style type="text/css">
body
{
font-family: 'helvetica neue';
color:#666;
padding: 0 20%;
}
li input
{
display: none;
}
li.edit span
{
display: none;
}
li.edit input
{
display: initial;
}
</style>
</head>
<body>
<header>
<h1>Javascript Events</h1>
</header>
<main>
<ul id='checkList'>
<li>
<span>apples</span>
<input value='Apples'/>
</li>
<li>
<span>Oranges</span>
<input value='Oranges'/>
</li>
<li>
<span>Bananas</span>
<input value='Bananas'/>
</li>
</ul>
</main>
<script type="text/javascript">
var checkList= document.getElementById('checkList');
var items=checkList.querySelectorAll('li');
var inputs=checkList.querySelectorAll('input');
for(var i=0;i<items.length;i++)
{
items[i].addEventListener('click',editItem);
inputs[i].addEventListener('blur',updateItem);
inputs[i].addEventListener('keypress',itemKeyPress);
}
function editItem(){
this.className='edit';
var input=this.querySelector('input');
input.focus();
input.setSelectionRange(0,input.value.length);
}
function updateItem()
{
this.previousElementSibling.innerHTML=this.value;
this.parentNode.className='';
}
function itemKeyPress(event)
{
if(event.which===13)
{
updateItem.call(this);
}
}
</script>
</body>
</html>
这份代码是看YouTube视频打下来的