一,在我们实现动态生成HTML代码时会出现,使用JQ方法会不被执行,解决方法,如下:使用jquery的委托事件,将该方法委托到页面已经存在的一个节点上

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="jquery-1.11.1.js"></script>
<script>
$(function () {
$("#demo").delegate('.t1', 'click', function () {
alert("t1");
});
$("#demo").delegate('#t2', 'click', function () {
alert("t2");
});
$("#demo").delegate('.t3', 'click', function () {
alert("t3");
});
$("#demo").delegate('#t4', 'click', function () {
alert("t4");
});
Test();
});
function Test() {
var strHtml = "<div class='t1'>t1</div>";
strHtml += "<div id='t2'>t2</div>";
strHtml += "<div class='t3'>t3</div>";
strHtml += "<div id='t4'>t4</div>";
$("#demo").html(strHtml);
}
</script>
</head>
<body>
<div id="demo">
</div>
<div id="demo1">
</div>
</body>
</html>

二,经过以上测试,得出

1》使用class和id触发的写法是一样的

2》JS动态添加的HTML并不需要在绑定事件的节点内,一样可以触发成功

最新文章

  1. Django 基础(一)
  2. tomcat详情
  3. Ubuntu下制作ISO文件
  4. C#入门篇6-8:字符串操作 深入研究字符串的内存驻留机制
  5. lintcode:最大子数组差
  6. struts2.1.3之后使用自定义Filter
  7. protubuffer for windows配置指南!
  8. many bugs report when test bcm bt/wifi chip
  9. hdu 2614
  10. Spring中各jar包的作用
  11. 关于spingMVC使用时配置自动扫描出现的路径报错
  12. yum仓库的定制
  13. python 小白(无编程基础,无计算机基础)的开发之路 day2
  14. tensorflow ckpt文件转caffemodel时遇到的坑
  15. python如何安装pip及venv管理
  16. 【easy】112.path sum 113.-----------------
  17. 项目管理-工作量评估 Manday
  18. iOS中时间与时间戳的相互转化
  19. python 取值方法:截取字符串
  20. Recover InnoDB dictionary

热门文章

  1. 关于Object.create方法
  2. 分页插件 Bootstrap Paginator
  3. fputc, fputs, putc, putchar, puts - 输出字符和字符串
  4. Nginx学习总结:常用module(二)
  5. java ArrayList练习题
  6. 判断是否为PC
  7. BZOJ4386 [POI2015]Wycieczki 矩阵+倍增
  8. python 的set定义
  9. Centos6安装zabbix-agent
  10. Component series.liquidFill not exists. Load it first.