<!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>
* {
padding: 0;
margin: 0;
}
.aaa {
border-bottom: 1px solid darkgrey;
float: left;
height: 39px;
line-height: 39px;
width: 100%;
}
.aaa:hover {
background: #3C8CBB;
color: white;
}
.aaa:hover .span-x {
color: white;
}
.span-x {
margin-left: 1%;
color: darkgrey;
padding-left: 14px;
padding-right: 14px;
cursor: pointer;
float: right;
}
#itemList {
overflow-y: scroll;
width: 410px;
margin-top: 5px;
height: 200px;
border: 1px solid darkgrey;
}
</style>
</head>
<body>
<input type="text" id="teacherInput">
<button id="itemAdd" style="background: white;border: 1px solid darkgrey;">点击添加</button>
<div id="itemList">
</body>
</html>
<script src="http://code.jquery.com/jquery-3.3.1.js"></script>
<script>
function myFun() {
var ipt = document.getElementById("teacherInput");
var btn = document.getElementById("itemAdd");
btn.onclick = function () {
if ($("#teacherInput").val() != "") {
var newObj = $("<div class='aaa'>" + '<span style="padding: 5px;">' + ipt.value + '</span>' + '<span class="span-x">X</span>' + "</div>");
$("#itemList").append(newObj);
$("#teacherInput").val("")
$(".span-x").click(function () {
$(this).parent().css("display", "none")
})
}
}
}
myFun();
</script>