Jquery Appended Content - Not Clickable

核能气质少年 提交于 2019-12-02 17:36:18

问题


I have the following JQ. It's basically adding a little icon that will allow for some inline editing when a list item is selected. However, I am unable to work with the jquery added content. I cant even log anything to console when I click my JQ added content. Is there something wrong with my code below?

I can not add a fiddle, because I dont have a link to the Kendo UI libraries, that this list is using.

  <script>
                $(function () {
                    $("#treeview-left li").click(function () {
                            $("div#EditEntity").remove();
                            $(this).find(".k-state-focused").append("<div id='EditEntity'>&nbsp;&nbsp;<a href='#' id='EditWindow'  class='icon-pencil active tiny'></a></div>");
                    });
                    $(".k-state-selected").on("click", "a#EditWindow", function (e) {
                        e.preventDefault();
                        $.get("ClassificationEditEntity", function (data) {
                            $(".k-window-content").html(data);
                        });
                    });
                });

            </script>

回答1:


you need delegated event as html is dynamically added after DOM load:

$(".k-state-focused").on("click", "a#EditWindow", function (e) {
  console.log("Asdf");
  $.get("ClassificationEditEntity", function(data) {
    $(".k-window-content").html(data);
  });
});

or:

$(document).on("click", "a#EditWindow", function (e) {
      console.log("Asdf");
      $.get("ClassificationEditEntity", function(data) {
        $(".k-window-content").html(data);
      });
    });

See HERE at the last of the page details of delegated events.



来源:https://stackoverflow.com/questions/24939966/jquery-appended-content-not-clickable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!