jQuery live with the ready or load event

后端 未结 3 1006
孤街浪徒
孤街浪徒 2020-12-20 17:19

I\'m using the jQuery Tools tooltip plugin, which is initialized with $(\'selector\').tooltip(). I\'d like to call this on any current or future .tooltipp

相关标签:
3条回答
  • 2020-12-20 17:35

    To add to HurnsMobile's excellent answer; Looking at bindReady(), which is the internal call that jQuery makes to bind to the document load event every time you call $(some_function) or $(document).ready(some_function) we see why we cannot bind to "ready":

    bindReady: function() {
            if ( readyBound ) {
                return;
            }
    
            readyBound = true;
    
            // Catch cases where $(document).ready() is called after the
            // browser event has already occurred.
            if ( document.readyState === "complete" ) {
                return jQuery.ready();
            }
    
            // Mozilla, Opera and webkit nightlies currently support this event
            if ( document.addEventListener ) {
                // Use the handy event callback
                document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false );
    
                // A fallback to window.onload, that will always work
                window.addEventListener( "load", jQuery.ready, false );
    
            // If IE event model is used
            } else if ( document.attachEvent ) {
                // ensure firing before onload,
                // maybe late but safe also for iframes
                document.attachEvent("onreadystatechange", DOMContentLoaded);
    
                // A fallback to window.onload, that will always work
                window.attachEvent( "onload", jQuery.ready );
    
                // If IE and not a frame
                // continually check to see if the document is ready
                var toplevel = false;
    
                try {
                    toplevel = window.frameElement == null;
                } catch(e) { //and silently drop any errors 
                        }
                        // If the document supports the scroll check and we're not in a frame:    
                if ( document.documentElement.doScroll && toplevel ) {
                    doScrollCheck();
                }
            }
        }
    

    To sum it up, $(some_function) calls a function which binds to:

    • DOMContentLoaded
    • onreadystatechange (DOMContentLoaded)
    • window.load / onload

    Your best bet would be to bind to those actions that might create new .tooltipper elements, rather than trying to listen for the ready event (which happens only once).

    0 讨论(0)
  • 2020-12-20 17:37

    HurnsMobile is right. JQuery live does not support the ready-event.

    This is why I created a plugin that combines the two. You register your callback once, and then you will need to call the plugin once for content you add manually.

    $.liveReady('.tooltipper', function(){
      this.tooltip()
    });
    

    Then when creating new content:

    element.html(somehtml);
    element.liveReady();
    

    or

    $('<div class="tooltipper">...').appendTo($('body')).liveReady();
    

    A demo is available here: http://cdn.bitbucket.org/larscorneliussen/jquery.liveready/downloads/demo.html

    Check out the introductory post here: http://startbigthinksmall.wordpress.com/2011/04/20/announcing-jquery-live-ready-1-0-release/


    Also have a look at http://docs.jquery.com/Plugins/livequery, which listenes for changes on the dom.

    0 讨论(0)
  • 2020-12-20 18:01

    Quoted from the jQ API (http://api.jquery.com/live/):

    In jQuery 1.3.x only the following JavaScript events (in addition to custom events) could be bound with .live(): click, dblclick, keydown, keypress, keyup, mousedown, mousemove, mouseout, mouseover, and mouseup.

    As of jQuery 1.4 the .live() method supports custom events as well as all JavaScript events.

    As of jQuery 1.4.1 even focus and blur work with live (mapping to the more appropriate, bubbling, events focusin and focusout).

    As of jQuery 1.4.1 the hover event can be specified (mapping to "mouseenter mouseleave").

    .live() does not appear to support the ready event.

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