javascript——元素的增删(类似表格的行添加)

案例演示

在这里插入图片描述

案例代码

<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Document</title>
		<script src="js/jquery-3.4.1.min.js"></script>
		<style>
			.box {
				width: 800px;
			}

			.item {
				width: 800px;
				display: flex;
				align-items: center;
				background-color: #F3F4F4;
				height: 60px;
				margin-bottom: 10px;
				position: relative;
				padding: 0 5px;
			}

			.btIcon {
				width: 30px;
				height: 20px;
				vertical-align: middle;
			}

			#keyWord {
				outline: none;
				border: 1px solid #666666;
				height: 28px;
			}

			#keyWord:focus {
				border: 1px solid skyblue;
			}

			.numSelect {
				margin-left: 20px;
			}

			.add {
				width: 140px;
				text-align: center;
				color: #ffffff;
				cursor: pointer;
				background: #6cc644;
				padding: 5px;
				border-radius: 5px;
				font-size: 14px;
			}

			.add:hover {
				background-color: #000000;
			}

			.delete {
				position: absolute;
				right: 20px;
				top: 15px;
				width: 50px;
				height: 30px;
				background-color: #6cc644;
				color: #ffffff;
				border: none;
				outline: none;
				font-size: 14px;
				display: none;
			}
		</style>
	</head>
	<body>
		<div class="box">
			<div class="item" id="unique">
				<div class="tips">
					<img src="img/bitian.png" alt="必填" class="btIcon">
					<label for="keyWord">关键词填写<text class="xuhao">1</text></label>
					<input type="text" id="keyWord" name="myname" class="shuru">
				</div>
				<div class="numSelect">
					<label for="">选择分类</label>
					<select name="orderNum">
						<option value="1">1</option>
						<option value="2">2</option>
					</select>
				</div>
				<button class="delete">删除</button>
			</div>
		</div>
		<div class="add">+增加搜索关键词</div>
	</body>
	<script>
		let domBox;
		$(document).on("click", ".add", function() {
			let domBox = $("#unique").clone().removeAttr("id")
			$(".box").append(domBox)
			var i = 1;
			var y = 0;
			$(".xuhao").each(function() { //增加一行后重新更新序号1,2,3......
				$(this).html(i++);
			})
			$(".shuru").each(function() {
				$(this).attr("name", "myname" + y)
				y++
			})
			$('.delete').show()
			$("#unique").find('.delete').hide()
		})
		$(document).on("click", ".delete", function() {
			$(this).parent().remove()
			var i = 1;
			var y = 0;
			$(".xuhao").each(function() { //删除后更新序号
				$(this).html(i++);
			})
			$(".shuru").each(function() {
				$(this).attr("name", "myname" + y)
				y++
			})
		})
	</script>
</html>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值