jQuery-给ul添加了li之后,添加的li并没有绑定点击监听怎么办?

我们看一个简单的例子

效果图如下
这里面有一个ul里面套着4个li,还有一个独立的li
在这里插入图片描述
代码实例:
需求
1.点击li,背景就会变成红色
2.点击btn,就会添加一个li

<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'practice_02.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>	
  	<ul>
  		<li>111</li>
  		<li>222</li>
  		<li>333</li>
  		<li>444</li>
  	</ul>
  	<li>222</li>
  	
  	<button id="btn">添加一个li</button>
  	<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
	<script type="text/javascript">
		//1.点击li,背景就会变成红色
		$('ul>li').click(function() {
			//第一种写法
			this.style.background = 'red';
			//第二种写法          $(this).css('background','red');
		});
		
		//2.点击btn,就会添加一个li
		$('#btn').click(function() {
			$('ul').append('<li>222</li>');
		});
	</script>
  </body>
</html>

效果如下:
我们会发现,添加之后的li点击了没有变化,说明添加之后的li并没有绑定点击监听
在这里插入图片描述

解决方法一(普通方法):

<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'practice_02.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<style type="text/css">
	
 	</style>
  </head>
  
  <body>	
  	<ul>
  		<li>111</li>
  		<li>222</li>
  		<li>333</li>
  		<li>444</li>
  	</ul>
  	<li>222</li>
  	
  	<button id="btn">添加一个li</button>
  	<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
	<script type="text/javascript">
		//1.点击li,背景就会变成红色
		$('ul>li').click(function() {
			//第一种写法
			this.style.background = 'red';
			//第二种写法          $(this).css('background','red');
		});
		
		//2.点击btn,就会添加一个li
		$('#btn').click(function() {
			$('ul')
			.append('<li>222</li>')//这里我们会发现,当我们添加了一个li之后,点击它背景颜色并不会改变
			.children('li')//找到ul的孩子li
			.click(function() {//添加点击事件
				this.style.background = 'red';
			});
		});
	</script>
  </body>
</html>

方法二(事件委托):

1.简单讲解

(1)事件委托(代理/委派):
->将多个子元素(li)的事件监听委托给父辈元素(ul)处理
->监听回调是加在了父辈元素上
->当操作任何一个子元素(li)时,事件会冒泡到父辈元素(ul)
->父辈元素不会直接处理事件,而是根据event.target得到发生事件的子元素(li),通过这个子元素调用事件回调函数
(2)事件委托的双方

->委托方: 业主li
->被委托方: 中介ul

(3)使用事件委托的好处

->添加新的子元素,自动有事件响应处理
->减少事件监听的数量

(4)jQuery事件委托API

->设置事件委托:$(parentSelector).delegate(childrenSelector,eventName,callback)

->移出事件委托:$(parentSelector).undelegate(eventName)

2.代码实例

<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'practice_02.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<style type="text/css">
	
 	</style>
  </head>
  
  <body>	
  	<ul>
  		<li>111</li>
  		<li>222</li>
  		<li>333</li>
  		<li>444</li>
  	</ul>
  	<li>222</li>
  	
  	<button id="btn">添加一个li</button>
  	<script src="https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script>
	<script type="text/javascript">
		//1.点击li,背景就会变成红色
		$('ul>li').click(function() {
			//第一种写法
			this.style.background = 'red';
			//第二种写法          $(this).css('background','red');
		});
		
		//2.点击btn,就会添加一个li
		$('#btn').click(function() {
			$('ul').append('<li>222</li>');
		});
		//事件委托
		$('ul').delegate('li','click',function() {
			this.style.background = 'red';
		});
		
		
	</script>
  </body>
</html>

	

3.效果
请读者复制代码自行测试
在这里插入图片描述

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梁同学与Android

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值