jQuery equivalent of body onLoad

前端 未结 6 791
梦毁少年i
梦毁少年i 2020-12-29 04:37

Is it allowed to use along with jQuery\'s document.ready() handlers? I can\'t find a way to achieve the same funct

相关标签:
6条回答
  • 2020-12-29 05:08

    $(window).load(myfunc) is what you're looking for. If you're working with the load event of an iframe, you can replace window with the iframe element/selector.

    0 讨论(0)
  • Found this thread updating some old code that had <body onload="someFunc()"> so to give an updated answer:

    Don't use load(). While $(document).ready(function(){...}); should work (depending on your use case) you actually want: $(window).on('load', function(){...}); to more closely mimic <body onload="...">

    Some threads suggest this has been deprecated since jQuery 1.8 (as well as .unload() and .error()) and should be replaced with proper .on() implementations. I've confirmed that using load() on 3.3.1 results in an error, namely: indexOf is not a function in jquery.min.js

    Documentation For jQuery's onEvent Handler

    0 讨论(0)
  • 2020-12-29 05:13

    Try load():

    jQuery(document).load(myfunc)
    
    0 讨论(0)
  • 2020-12-29 05:16

    This works as well:

    $(window).load(function(){
        // ..
        myfunc();
        // ..
    });
    
    0 讨论(0)
  • 2020-12-29 05:22

    .load(), .unload() or .error() are deprecated since jQuery 1.8. Lookup for these aliases in your code and replace them with the .on() method instead

    $(window).on('load', function(){...})
    
    0 讨论(0)
  • 2020-12-29 05:23

    From the jQuery API on the ready handler:

    The .ready() method is generally incompatible with the <body onload=""> attribute. If load must be used, either do not use .ready() or use jQuery's .load() method to attach load event handlers to the window or to more specific items, like images.

    0 讨论(0)
提交回复
热议问题