1. <legend id='FrUyN'><style id='FrUyN'><dir id='FrUyN'><q id='FrUyN'></q></dir></style></legend>
  2. <i id='FrUyN'><tr id='FrUyN'><dt id='FrUyN'><q id='FrUyN'><span id='FrUyN'><b id='FrUyN'><form id='FrUyN'><ins id='FrUyN'></ins><ul id='FrUyN'></ul><sub id='FrUyN'></sub></form><legend id='FrUyN'></legend><bdo id='FrUyN'><pre id='FrUyN'><center id='FrUyN'></center></pre></bdo></b><th id='FrUyN'></th></span></q></dt></tr></i><div id='FrUyN'><tfoot id='FrUyN'></tfoot><dl id='FrUyN'><fieldset id='FrUyN'></fieldset></dl></div>

  3. <small id='FrUyN'></small><noframes id='FrUyN'>

      • <bdo id='FrUyN'></bdo><ul id='FrUyN'></ul>
      <tfoot id='FrUyN'></tfoot>

      jQuery函数在append()之后没有响应

      时间:2023-10-20
          <bdo id='Ihi6i'></bdo><ul id='Ihi6i'></ul>
          <legend id='Ihi6i'><style id='Ihi6i'><dir id='Ihi6i'><q id='Ihi6i'></q></dir></style></legend>
            <tbody id='Ihi6i'></tbody>

        • <tfoot id='Ihi6i'></tfoot>
          <i id='Ihi6i'><tr id='Ihi6i'><dt id='Ihi6i'><q id='Ihi6i'><span id='Ihi6i'><b id='Ihi6i'><form id='Ihi6i'><ins id='Ihi6i'></ins><ul id='Ihi6i'></ul><sub id='Ihi6i'></sub></form><legend id='Ihi6i'></legend><bdo id='Ihi6i'><pre id='Ihi6i'><center id='Ihi6i'></center></pre></bdo></b><th id='Ihi6i'></th></span></q></dt></tr></i><div id='Ihi6i'><tfoot id='Ihi6i'></tfoot><dl id='Ihi6i'><fieldset id='Ihi6i'></fieldset></dl></div>

              <small id='Ihi6i'></small><noframes id='Ihi6i'>

              • 本文介绍了jQuery函数在append()之后没有响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在创建一系列 div 框,让用户可以使用 jQuery 从每个框中添加/删除项目.我发现在一个盒子里添加一个新元素后,我绑定到那个元素的点击函数没有响应.我的代码大致如下:

                I'm creating a series of div boxes that lets users add/remove items from each box with jQuery. I find that after I add a new element to a box, the click function I have bound to that element will not respond. Here's roughly what my code looks like:

                $(".add").click(function() {
                $("#targetbox").append("<span class='remove'>This element was added</span>");
                });
                
                $(".remove").click(function() {
                alert("removing");
                $(this).remove();
                });
                

                如果我用项目预先填充#targetbox,它们会响应点击功能.只有动态添加的项目不响应函数.

                If I pre-populate #targetbox with items, they respond to the click function. It's only the items that are dynamically added that do not respond to the function.

                推荐答案

                将click方法直接添加到新添加的元素中

                Add the click method directly to your newly appended element

                $(".add").click(function() {
                    $("#targetbox").append("<span class='remove'>This element was added</span>")
                    .bind("click",function(e) {
                        alert("removing");
                        $(this).remove();
                    });
                });
                

                或者使用 .live() 方法在添加任何新的 .remove 元素后为您绑定点击事件

                Or use the .live() method that will bind the click event for you after appending any new .remove elements

                $(".add").click(function() {
                    $("#targetbox").append("<span class='remove'>This element was added</span>");
                });
                
                $(".remove").live("click", function() {
                    alert("removing");
                    $(this).remove();
                });
                

                这篇关于jQuery函数在append()之后没有响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:jQuery 追加 CSS 文件并等待加载? 下一篇:如何修复“未捕获的类型错误:无法调用 null 的方法‘appendChild’"

                相关文章

              • <i id='BcQ2X'><tr id='BcQ2X'><dt id='BcQ2X'><q id='BcQ2X'><span id='BcQ2X'><b id='BcQ2X'><form id='BcQ2X'><ins id='BcQ2X'></ins><ul id='BcQ2X'></ul><sub id='BcQ2X'></sub></form><legend id='BcQ2X'></legend><bdo id='BcQ2X'><pre id='BcQ2X'><center id='BcQ2X'></center></pre></bdo></b><th id='BcQ2X'></th></span></q></dt></tr></i><div id='BcQ2X'><tfoot id='BcQ2X'></tfoot><dl id='BcQ2X'><fieldset id='BcQ2X'></fieldset></dl></div>

                  <small id='BcQ2X'></small><noframes id='BcQ2X'>

                  <legend id='BcQ2X'><style id='BcQ2X'><dir id='BcQ2X'><q id='BcQ2X'></q></dir></style></legend>
                    <bdo id='BcQ2X'></bdo><ul id='BcQ2X'></ul>
                1. <tfoot id='BcQ2X'></tfoot>