Is there an equivalent to $ (document) .ready for initialization after reloading part of the page?

I have the following pretty standard process:

  • I initialize my page to $(document).ready. This includes binding events to items in the table.
  • The contents of the table are then updated dynamically through an ajax call.

Now I need to reorder the events for the contents of the table. Is there a standard way to do this? those. Is there an equivalent $(document).readythat starts after updating the DOM of a partial page?

Thanks.

+3
source share
2 answers

Look at event delegation using live . From the docs:

"" , ( ). , "li" - click ( , ).

:

$('#myTable td').live("click", function() {
    alert('hello!');
});

(), , . live :

(, ) - .

, , :

function bindStuffToTable()
{
    $('#myTable td').click(function() {
       alert('Hello!');
    });
}

$('#myTable').load('/some/link', bindStuffToTable);

, :

$('#someButton').click(function() {
    //replaces the contents of someDiv with the table generated by foo.php
    $('#someDiv').load('foo.php', bindStuffToTable);
});
+14

$(document).ready(), ...

AJAX

<script type="text/javascript">
    $(document).ready(function() {
        alert($('div.partialresponse').html());
        //Alerts 'content'
    });
</script>
<div class="partialresponse">
   content
</div>
+1

Source: https://habr.com/ru/post/1710666/


All Articles