The JS file doesn't work in partial view after it refreshed

前端 未结 2 1998
日久生厌
日久生厌 2021-01-18 11:51

This is branch question of \"Js file not loaded after partial view is refreshed\". The broblem is that if I put my script in to the main view it doesn\'t work in partial. My

2条回答
  •  -上瘾入骨i
    2021-01-18 12:11

    Use a delegated event handler:

    $(document).on('click', "[data-hide]", function () 
    

    The jQuery selector is only run at event time, so it will work with dynamically added elements.

    It works by listening for the event at a non-changing ancestor element (document is the safest default if nothing else is closer/convenient). It then applies the selector to the elements in the bubble-chain. It then applies your event handler function to only the matching elements that caused the event.

    This is very efficient compared to connecting event handlers to individual elements and any speed difference is negligible as you simply cannot click fast enough to notice :)

提交回复
热议问题