jquery添加的addClass添加点击事件没有效果

云教主 2017-06-29 10:11:38
jquery添加的addClass添加点击事件,也用on绑定了click,还是没有效果
$('.toolbar .switch').click(function(){
$('.toolbar .switch').toggleClass('chevron-left');
$('.toolbar .switch').toggleClass('th-list');
})
$('.toolbar .switch').on('click','.chevron-left',function(){
alert()
});
$('.toolbar .switch').on('click','.th-list',function(){
alert()
})
...全文
519 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Go 旅城通票 2017-06-29
  • 打赏
  • 举报
回复
$('.toolbar .switch').on
改为
$('.toolbar').on

on参数有选择器的话$要选出父对象,而不是本身
云教主 2017-06-29
  • 打赏
  • 举报
回复
谁来帮忙解决一下呀,用的这个版本jquery-1.11.3.min.js
香蕉猪 2017-06-29
  • 打赏
  • 举报
回复
既然是jquery添加的addClass那么添加的点击事件也要同时写在添加此class的function内 e.g.:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
	<meta name="x5-orientation"content="portrait">
	<title></title>
	<style>
	
	</style>
</head>
<body>
	<div>1234</div>
	<input type="button" value="点击"/>
	<script src="./jquery-1.11.3.min.js"></script>
	<script>
	$(function(){
		$("input").click(function(){
			$("div").addClass("newClass");
			$(".newClass").unbind('click').on('click',function(){
				alert("hello?");
			});
		});
	});
	
	</script>
</body>
</html>

87,902

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧