简体   繁体   English

JQuery on() 不会在附加元素上触发

[英]JQuery on() does not fire on appended element

My event handler on a child element that is appended to parent is not invoked.未调用附加到父元素的子元素上的事件处理程序。 Event handler on parent is invoked.调用父级上的事件处理程序。

componentDidMount() {

  $(document).ready(()=> {

    $(".parent").on("click", ".child", (e) => {
      e.preventDefault();
      $(this).parent('div').doStuff();
    });

    $(".parent").click(function(e) {
      console.log("this is working");
      $(".parent").append('<div><a href="#" className="child">Do Stuff</a></div>'); 
    });

  }
}

Problem was that i was using className which is React specific way to refer to HTML element's class attribute.问题是我使用的是类名,这是 React 特定的方式来引用className元素的class属性。 When I made this update, it worked.当我进行此更新时,它起作用了。

$(".parent").append('<div><a href="#" class="child">Do Stuff</a></div>');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM