input的val添加到元素内可删除

本文介绍了一个简单的HTML页面,该页面通过使用jQuery来实现输入框与按钮的交互功能。用户可以在输入框中输入教师名称,并通过点击按钮将名称添加到页面下方的列表中。此外,还提供了一个删除按钮以便移除列表项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值