Add a listener to an element that is dynamically added

后端 未结 2 1858
抹茶落季
抹茶落季 2021-01-25 03:11

I have a javascript/jQuery page where I\'m using Twitter Bootstrap\'s typeahead. There are two separate places in the javascript where I add elements that I would like to be ty

2条回答
  •  花落未央
    2021-01-25 03:46

    You can use jQuery delegation to do that. For example:

    $("#element-that-will-contain-typeaheads").on("DOMNodeInserted", function () {
        $(this).find(".typeahead").typeahead();
    });
    

提交回复
热议问题