• jQuery动态添加元素后,元素注册事件失效


    近日丢酱在学jQuery,在学习的时候遇到了一个问题,虽然最后解决了,但是丢酱觉得要把这个问题解决的方法记录下来,这样有小伙伴遇到同样的问题就不会跟我一样解决好久啦。
    我先贴一段正常逻辑的代码

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. <script src="jquery.min.js">script>
    9. <style>
    10. * {
    11. margin: 0;
    12. padding: 0;
    13. }
    14. a {
    15. text-decoration: none;
    16. }
    17. button {
    18. position: relative;
    19. left: 712px;
    20. top: 97px;
    21. }
    22. .content {
    23. width: 500px;
    24. height: 30px;
    25. line-height: 30px;
    26. border: 1px solid #bbb;
    27. margin: 100px auto;
    28. border-collapse: collapse;
    29. }
    30. th {
    31. background-color: rgb(27, 126, 184);
    32. color: #fff;
    33. border: 1px solid #bbb;
    34. }
    35. td {
    36. border: 1px solid #bbb;
    37. }
    38. style>
    39. head>
    40. <body>
    41. <button>添加数据button>
    42. <table class="content" align="center">
    43. <tr>
    44. <th style="font-weight: 700;">课程名称th>
    45. <th style="font-weight: 700;">所属学院th>
    46. <th style="font-weight: 700;">已学会th>
    47. tr>
    48. table>
    49. <script>
    50. $(function() {
    51. var t1 = $('javaScript传智播客-前端与移动开发学院GET');
    52. var t2 = $('css传智播客-前端与移动开发学院GET');
    53. var t3 = $('html传智播客-前端与移动开发学院GET');
    54. var t4 = $('jQuery传智播客-前端与移动开发学院GET');
    55. $('button').on('click', function() {
    56. // console.log($('table tr'));
    57. if ($('table tr').length === 1) {
    58. $('table').append(t1);
    59. console.log($(".del"));
    60. // console.log($('table tr').length);
    61. } else if ($('table tr').length === 2) {
    62. $('table').append(t2);
    63. } else if ($('table tr').length === 3) {
    64. $('table').append(t3);
    65. } else if ($('table tr').length === 4) {
    66. $('table').append(t4);
    67. } else {
    68. return false;
    69. }
    70. });
    71. // $("table a").on('click', function() {
    72. // $('table').children('tr').remove();
    73. // })
    74. $('tr').on('click', 'a', function() {
    75. console.log(11);
    76. $(this).parents('tr').remove();
    77. })
    78. })
    79. script>
    80. body>
    81. html>

    前端页面操作流程:

    1.先点击“添加数据”按钮,把a标签动态生成出来。

    2.再点击a标签(GET),查看控制台。

    结果:控制台啥也没有输出

    因为这样写是有问题的。所有需要修改一下。我先把我修改后的代码贴出来。后面解释

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Documenttitle>
    8. <script src="jquery.min.js">script>
    9. <style>
    10. * {
    11. margin: 0;
    12. padding: 0;
    13. }
    14. a {
    15. text-decoration: none;
    16. }
    17. button {
    18. position: relative;
    19. left: 712px;
    20. top: 97px;
    21. }
    22. .content {
    23. width: 500px;
    24. height: 30px;
    25. line-height: 30px;
    26. border: 1px solid #bbb;
    27. margin: 100px auto;
    28. border-collapse: collapse;
    29. }
    30. th {
    31. background-color: rgb(27, 126, 184);
    32. color: #fff;
    33. border: 1px solid #bbb;
    34. }
    35. td {
    36. border: 1px solid #bbb;
    37. }
    38. style>
    39. head>
    40. <body>
    41. <button>添加数据button>
    42. <table class="content" align="center">
    43. <tr>
    44. <th style="font-weight: 700;">课程名称th>
    45. <th style="font-weight: 700;">所属学院th>
    46. <th style="font-weight: 700;">已学会th>
    47. tr>
    48. table>
    49. <script>
    50. $(function() {
    51. var t1 = $('javaScript传智播客-前端与移动开发学院GET');
    52. var t2 = $('css传智播客-前端与移动开发学院GET');
    53. var t3 = $('html传智播客-前端与移动开发学院GET');
    54. var t4 = $('jQuery传智播客-前端与移动开发学院GET');
    55. $('button').on('click', function() {
    56. // console.log($('table tr'));
    57. if ($('table tr').length === 1) {
    58. $('table').append(t1);
    59. console.log($(".del"));
    60. // console.log($('table tr').length);
    61. } else if ($('table tr').length === 2) {
    62. $('table').append(t2);
    63. } else if ($('table tr').length === 3) {
    64. $('table').append(t3);
    65. } else if ($('table tr').length === 4) {
    66. $('table').append(t4);
    67. } else {
    68. return false;
    69. }
    70. });
    71. // $("table a").on('click', function() {
    72. // $('table').children('tr').remove();
    73. // })
    74. $('.content').on('click', 'a', function() {
    75. console.log(11);
    76. $(this).parents('tr').remove();
    77. })
    78. })
    79. script>
    80. body>
    81. html>

    前端页面操作流程:

    1.先点击“添加数据”按钮,把a标签动态生成出来。

    2.再点击a标签(GET),查看控制台。

    **结果:控制台开始输出了 **

    开始作解释了。其实大家可以看出来,这里只有一个区别。我把这两个贴出来

    1. // 控制台不输出
    2. // 通过on事件委派来给tr里面的a标签添加点击事件
    3. $('tr').on('click', 'a', function() {
    4. console.log(11);
    5. $(this).parents('tr').remove();
    6. });
    7. // 控制台输出
    8. $('.content').on('click', 'a', function() {
    9. console.log(11);
    10. $(this).parents('tr').remove();
    11. });

    第一个里,我们是事件委派对a标签的元素作单击事件。但是这种jquery获取不到动态添加元素的。

    方案解释:

    on虽然可以给未来动态创建的元素绑定事件,但是要先获取已拥有的父级元素。然后才能再获取动态元素。

    就是相当于说,给父元素添加一个事件(该事件类型和动态元素想要实现的事件类型一样)

    所以这里可以得到:

    1. <table class="content" align="center">
    2. <tr>
    3. <th style="font-weight: 700;">课程名称th>
    4. <th style="font-weight: 700;">所属学院th>
    5. <th style="font-weight: 700;">已学会th>
    6. tr>
    7. // 动态添加的元素
    8. <tr align="center">
    9. <td>javaScripttd>
    10. <td>传智播客-前端与移动开发学院td>
    11. <td><a href="javascript:;" class="del">GETa>td>
    12. tr>
    13. table>

    .content是a标签的父级

    1. $('.content').on('click', 'a', function() {
    2. console.log(11);
    3. $(this).parents('tr').remove();
    4. })

    只要记住,在动态生成的元素中,不能直接指定其事件。只能其父标签的事件后,过滤指定特定元素事件。

    还有一种写法:直接指定dom的元素事件。这也是可以的。

    1. $(document).on('click',"a",function(){
    2. console.log("22");
    3. })

  • 相关阅读:
    公司安防工程简要介绍及系统需求分析
    SpringBoot SpringBoot 基础篇(第一篇) 第1章 SpringBoot 入门 1.3 SpringBoot 快速入门
    Tomcat8 WEB-INF更改class后 用manager实现reload
    Nginx 优化
    吉利远景s1
    vue项目性能优化:去除没有引用的文件
    基于 LLM 的知识图谱另类实践
    计算机网络基础知识
    电脑重装系统后如何把网站设为首页
    【电驱动】永磁同步电机工作原理及分类
  • 原文地址:https://blog.csdn.net/DIUDIUjiang/article/details/126284588