jQuery [removed]() child selector

前端 未结 3 946
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-28 18:05

I have a ul and i want create an alert when i click on a child li.

    3条回答
    •  花落未央
      2021-01-28 18:47

      You can use

      $(document).on('click','#myul li', eventHandler) 
      

      which means you're registering an event handler on the document but eventHandler will be invoked only when the event originated from #myul li i.e for any li child of myul, not just the immediate child, can be a descendant as well which fits your case.

      But I suggest that you even reduce the scope of the event registration to the closest ancestor of li like below.

      $('#myul').on('click','li', eventHandler)
      

    提交回复
热议问题