简体   繁体   中英

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. When I made this update, it worked.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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