jquery动态绑定事件处理一些由于html代码是动态生成绑定事件失败问题

教程分享 > Java教程 > 博文分享 (1846) 2023-03-28 11:29:14
jquery动态绑定事件处理一些由于html代码是动态生成绑定事件失败问题
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>测试</title>
</head>
<body>
<div id="test_div"></div>

<!-- 引入jquery,这里需要改成你们自己的路径 -->
<script type="text/ecmascript" src="resources/common/js/include.js"></script>
<script type="text/javascript">
$(function(){
	var html='<button class="test_btn">动态生成按钮</button>';
	//动态代码添加到div中
	$('#test_div').append(html);
	
	//普通绑定事件方式绑定刚添加的代码中的button点击事件
	 $('.test_btn').click(function(){
		 alert("按钮被点击到了");
	 });
	
	//动态绑定,其实就是将绑定事件交给document托管;格式  $(document).on('事件名','jquery选择器',function(){});
	$(document).on('click','.test_btn',function(){
		alert('动态绑定生效了');
	})
})
</script>	
</body>
</html>
 

相关文章
ckeditor绑定keyup/keydown等事件实现一些功能,比如实时保存$(function(){//1编辑器初始化$('textarea#blog_content').ckeditor();
jquery绑定鼠标滚轮滚动事件 $(document).on("mousewheel DOMMouseScroll", function (event) {
在ckeditor4的时候绑定blur事件代码如下:window.editor.on('blur',function(){//这里写你想做的事儿})同样的代码搬到ckeditor5不仅不能用,还报错。
在Java编程中,ORM框架MyBatis处理@Select注解中的动态sql处理方法
Spring Boot 2.0 绑定properties属性资源文件 Spring Boot 2.0 读取properties配置文件值 Spring Boot 2.0获取properties配...
HTML 标签中onclick事件中的this并非当前元素。而是window。需要获取当前的元素怎么办呢?以下开始讲解。
jquery中的foreach(即each)数据的遍历或者处理HTML页面元素的遍历使用方法讲解
bootstrap-select ajax动态添加值中遇到的IE兼容问题解决
在使用bootstrap-table过程中,我们可能会遇到列属性events事件不能生效的问题,并会发现这个问题有时存在有时却又不存在,这是什么原因呢?其实造成events事件失效的原因非常简单...
ie9 jquery ajax跨域问题解决, ajax ie9 跨域问题解决,jquery,ajax,ie9
Java编程中spring boot项目动态添加拦截器Interceptor
问题描述 spring boot 2.6.x 项目启动报错 The elements [xxx,xxx] were left unbound 细节日志就是spring boot...
javascript中onclick事件传递对象参数,javascript,javascript传递对象参数