JQuery bind event after adding element dynamic

后端 未结 3 1401
广开言路
广开言路 2021-01-25 00:10

I have an issue after adding a element dynamically it doesn\'t have the click event, so i have the following:

$(\".myclass > li\").click(function () {
    ...         


        
3条回答
  •  春和景丽
    2021-01-25 01:04

    You need to use Event Delegation. You have to use .on() using delegated-events approach.

    i.e.

    $(document).on('event','selector',callback_function)
    

    In your case

    $(document).on('click', '.myclass > li', function () {
        ...
    });
    

    OR if you want to apply to ALL list items:

    $(".myclass").on('click', '> li', function () {
        ...
    });
    

提交回复
热议问题