How to scroll parent page with iFrame reload

2019-06-25 23:41发布

问题:

I have a page with a long iframe on it, which loads a schedule display with pages of various lengths. When a new page is displayed in the iframe though, the main page is often still showing the bottom of the page (lots of whitespace). How can I have the main page scroll to the top anytime a new page is loaded in the iframe?

This is the page

I've tried this is jQuery (on the parent page):

<script type="text/javascript">
  jQuery(document).ready(function() {
    $('#body iframe').load(function(){
     $(window).scrollTop(0);
    });
  }
</script>

Thanks!

回答1:

You're missing the closing ) for the .ready() call, and you shouldn't have a # before body.

Try this:

jQuery(document).ready(function() {
    $('body iframe').load(function(){
         $(window).scrollTop(0);
    });
});

With those corrections, the window jumps back to the top when the new content loads.