jQuery练习二:会员信息模块

一.需求

在这里插入图片描述

二.实现思路

1.table +css+背景图 实现表格。
2.点击按钮,生成新的tr节点并追加到table中。
3.点击删除图标,将所在的tr节点元素从table中移除。

三.代码实现

1.实现表格

<table cellpadding="10" class="table">
	<tr>
		<th>姓名</th>
		<th>性别</th>
		<th>卡号</th>
		<th>会员级别</th>
		<th>电话号码</th>
		<th>出生年月</th>
		<th>消费金额</th>
		<th style="width: 80px;" " colspan=" 2">操作</th>
	</tr>
	<tr class="info">
		<td>张三</td>
		<td></td>
		<td>62018549552731</td>
		<td>高级</td>
		<td>18486145748</td>
		<td>2020-01-02</td>
		<td>8,000,000</td>
		<td></td>
		<td class="delete"></td>
	</tr>
</table>
<button type="button" class="addbtn">Add</button>
		


为了复习下css随手写了一个,嫌麻烦的可以直接用UI组件

<style>
		table {
			width: 820px;
			margin: 10px;
		}
	
		table tr td {
			text-align: center;
		}
	
		table tr th {
			background-color: grey;
		}
	
		tr td:nth-child(8) {
			background-image: url(1.png);
			background-position: center;
			background-repeat: no-repeat;
			background-size: 30px 30px
		}
	
		tr td:nth-child(9) {
			background: url(2..png);
			background-position: center;
			background-repeat: no-repeat;
			background-size: 30px 30px
		}
		.addbtn{
			width: 80px;height: 38px;color: #fff;background-color: #f00;
			border: none;
		}
	</style>

2.点击按钮新增会员信息

<script>

			//实现点击按钮添加tr节点
			$("button.addbtn").on("click", function() {
				i += 1;
				let sex =  infomation("男","女");
				let infoclass = infomation("初级","中级");
				// console.log(num)
				
				$(".table").append(
					`<tr class="info">class="info"
				<td>张三${i}</td>
				<td>${sex}</td>
				<td>62018549552731</td>
				<td>${infoclass}</td>
				<td>18486145748</td>
				<td>2020-01-02</td>
				<td>8,000,000</td>
				<td></td>
				<td class="delete"></td>
			</tr>`
				)

		</script>

3.删除会员信息

这里通过this指向所在的信息行,然后通过closrst()方法来获取父元素

//点击x删除当前的tr节点
$("td.delete").on("click", function() {
		// var $parent =  this.parent()
		// console.log(this.parents('.info'))//报错
		console.log(this.closest('.info'))
		this.closest('.info').remove()
	})
})

4.数据处理

			var i = 1;
			var sex = ""
			//封装一个函数通过获取随机数得到一个随机字符串
			function infomation(res1,res2){
				var num = parseInt(Math.random() * 2 + 1) //生成一个(0,3)之间的随机整数
				if (num == 1) {
					result = res1;
				} else if (num == 2) {
					result = res2;
				}
				return result;
			}

四.完整代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>会员信息模块</title>
		<script src="jquery-3.4.1.min.js"></script>
		<style>
			table {
				width: 820px;
				margin: 10px;
			}
		
			table tr td {
				text-align: center;
			}
		
			table tr th {
				background-color: grey;
			}
		
			tr td:nth-child(8) {
				background-image: url(1.png);
				background-position: center;/*背景图居中显示*/
				background-repeat: no-repeat;/*不平铺*/
				background-size: 30px 30px/*设置背景图大小*/
			}
		
			tr td:nth-child(9) {
				background: url(2..png);
				background-position: center;
				background-repeat: no-repeat;
				background-size: 30px 30px
			}
			.addbtn{
				width: 80px;height: 38px;color: #fff;background-color: #f00;
				border: none;
			}
		</style>
	</head>
	<body>
		<table cellpadding="10" class="table">
			<tr>
				<th>姓名</th>
				<th>性别</th>
				<th>卡号</th>
				<th>会员级别</th>
				<th>电话号码</th>
				<th>出生年月</th>
				<th>消费金额</th>
				<th style="width: 80px;" " colspan=" 2">操作</th>
			</tr>
			<tr class="info">
				<td>张三</td>
				<td></td>
				<td>62018549552731</td>
				<td>高级</td>
				<td>18486145748</td>
				<td>2020-01-02</td>
				<td>8,000,000</td>
				<td></td>
				<td class="delete"></td>
			</tr>
		</table>
		<button type="button" class="addbtn">Add</button>
		<script>
			"use strict"
			var i = 1;
			var sex = ""
			//封装一个函数通过获取随机数得到一个随机字符串
			function infomation(res1, res2) {
				var num = parseInt(Math.random() * 2 + 1) //生成一个(0,3)之间的随机整数
				let result = ""
				if (num == 1) {
					result = res1;
				} else if (num == 2) {
					result = res2;
				}
				return result;
			}


			$(function() {
				$("button.addbtn").on("click", function() {
					i += 1;
					let sex = infomation("男", "女");
					let infoclass = infomation("初级", "中级");
					// console.log(num)
					//实现点击按钮添加tr节点
					$(".table").append(
						`<tr class="info">
							<td>张三${i}</td>
							<td>${sex}</td>
							<td>62018549552731</td>
							<td>${infoclass}</td>
							<td>18486145748</td>
							<td>2020-01-02</td>
							<td>8,000,000</td>
							<td></td>
							<td class="delete"></td>
						</tr>`
					)
					//点击x删除当前的tr节点
					$("td.delete").on("click", function() {
						// var $parent =  this.parent()
						// console.log(this.parents('.info'))//报错
						console.log(this.closest('.info'))
						this.closest('.info').remove()
					})
				})
			})
		</script>
	</body>
</html>

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
jQuery是一种流行的JavaScript库,它简化了在网页上进行DOM操作和事件处理的编程过程。使用jQuery可以轻松地制作注册页面。 首先,我们需要在HTML文件中包含jQuery的引用。这可以通过在<head>标签中添加以下代码来完成: ``` <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> ``` 接下来,我们可以在JavaScript代码中使用jQuery来处理注册页面的逻辑。首先,我们可以为注册按钮添加一个点击事件的处理程序,以便在用户点击注册按钮时执行一些操作。我们可以使用以下代码来实现这一点: ``` $('#registerButton').click(function() { // 在此处添加注册逻辑 }); ``` 在注册逻辑中,我们可以获取用户在注册表单中输入的值,并进行一些验证。例如,我们可以检查用户名和密码是否符合要求。我们可以使用以下代码来获取输入值: ``` var username = $('#username').val(); var password = $('#password').val(); ``` 在获取输入值之后,我们可以使用条件语句来进行验证。例如,我们可以检查用户名和密码是否为空,并显示相应的错误信息。我们可以使用以下代码来实现: ``` if (username === '' || password === '') { $('#errorMessage').text('用户名和密码不能为空'); } ``` 如果用户输入的值符合要求,我们可以将注册信息提交到服务器,并显示成功消息。例如,我们可以使用以下代码来实现: ``` $.post('register.php', {username: username, password: password}, function(response) { if (response === 'success') { $('#successMessage').text('注册成功'); } else { $('#errorMessage').text('注册失败,请稍后再试'); } }); ``` 以上就是使用jQuery制作注册页面的基本步骤。通过使用jQuery的DOM操作和事件处理功能,我们可以轻松地实现交互式的注册页面。当然,这只是一个简单的示例,实际制作注册页面时还可以根据需求添加更多功能和验证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值