要求:
解答:
<!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>
input {
180px;
}
ul{
display: none;
margin-top: 0px;
padding: 0px;
}
li{
height: 20px;
line-height: 20px;
list-style-type: none;
180px;
margin-left: 0px;
padding-left: 5px;
border-bottom: 1px solid #ddd;
border-left:1px solid #ddd;
border-right: 1px solid #ddd;
}
li:hover>.delete {
display: block;
}
.delete {
color: blue;
display: none;
margin-left: 100px;
margin-top: -20px;
cursor: pointer;
}
</style>
</head>
<body>
<input type="text" />
<ul>
</ul>
<script>
let ul=document.getElementsByTagName("ul")[0];
function venter(){
let kvalue = document.getElementsByTagName("input")[0].value;
let span = document.createElement("span");
span.innerHTML = "删除";
span.onclick = function(){
this.parentNode.parentNode.removeChild(this.parentNode)
};
span.className = "delete";
let node=document.createElement("li"); //创建一个li节点
node.innerHTML = "<span>"+kvalue+"</span>";
node.appendChild(span);
ul.appendChild(node)
document.getElementsByTagName("input")[0].value = "";
}
document.onkeyup=function(e){
e=e||window.event;
e.preventDefault();
switch(e.keyCode){
case 13:
venter();
break;
}
}
document.getElementsByTagName("input")[0].onfocus = function(){
let ul=document.getElementsByTagName("ul")[0];
ul.style.cssText="display:block;";
}
</script>
</body>
</html>