最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - jQuery on load not firing (not Ajax) - Stack Overflow

programmeradmin1浏览0评论

At the outset, let me be clear I'm not trying to use load() in an Ajax context to load a remote resource.

I'm just trying to bind a function to an object that doesn't exist at page load time, such that I can do stuff to it when it does appear. I'm using jQuery 1.7

I have a form with class="contact-form"). This form is created on the fly, so it doesn't exist when document.ready() fires.

What I want to do is make some stuff happen when the form is created. Presumably there should be a "load" or "ready" or some such event fired when the thing is available. Under previous versions of jQuery I'd have used delegate() or live(); but these have been deprecated, and the current documentation says to use on( "load", handler ) or its shortcut, load(). I'm getting this from /.

All of the following have so far failed to work:

 $(".contact-form").load(function(){
    console.log("Hi there!");
  }); 

and

 $(".contact-form").on("load", function(){
    console.log("Hi there!");
  }); 

and, in a hail-mary based on ideas from Jquery event handler not working on dynamic content,

 $(document.body).on("load", ".contact-form", function(){
    console.log("Hi there!");
  });      

Any pointers appreciated.

At the outset, let me be clear I'm not trying to use load() in an Ajax context to load a remote resource.

I'm just trying to bind a function to an object that doesn't exist at page load time, such that I can do stuff to it when it does appear. I'm using jQuery 1.7

I have a form with class="contact-form"). This form is created on the fly, so it doesn't exist when document.ready() fires.

What I want to do is make some stuff happen when the form is created. Presumably there should be a "load" or "ready" or some such event fired when the thing is available. Under previous versions of jQuery I'd have used delegate() or live(); but these have been deprecated, and the current documentation says to use on( "load", handler ) or its shortcut, load(). I'm getting this from https://api.jquery./load-event/.

All of the following have so far failed to work:

 $(".contact-form").load(function(){
    console.log("Hi there!");
  }); 

and

 $(".contact-form").on("load", function(){
    console.log("Hi there!");
  }); 

and, in a hail-mary based on ideas from Jquery event handler not working on dynamic content,

 $(document.body).on("load", ".contact-form", function(){
    console.log("Hi there!");
  });      

Any pointers appreciated.

Share Improve this question edited May 23, 2017 at 10:33 CommunityBot 11 silver badge asked Mar 11, 2014 at 21:17 Sam MooreSam Moore 773 silver badges6 bronze badges 4
  • There's no such thing, some elements, such as images, iFrames etc. have a load event, but you can't bind that event on something that doesn't exists, and there's no such event for other elements, closest you'll get is mutation observers. – adeneo Commented Mar 11, 2014 at 21:21
  • From JQuery docs (Same link): This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object. – palanik Commented Mar 11, 2014 at 21:22
  • Above ments are correct. How is .contact-form inserted into the page? – Will Commented Mar 11, 2014 at 21:24
  • The form is assembled via some javascript that I don't have control over, in response to a user's clicking a button. – Sam Moore Commented Mar 11, 2014 at 21:30
Add a ment  | 

3 Answers 3

Reset to default 4

If you use .load() which is a shortcut for .on('load') called the load event, the matching element (form in this case) must exist at the time the page was loaded. jQuery < 1.7 had a .live() function which would listen for new elements dynamically added to the page, but it was removed in jQuery 1.7 for various reasons, performance being a major one.

Other options

jQuery LiveQuery is a plugin that sounds like it will do exactly what you're looking for. https://github./brandonaaron/livequery

jQuery Entwine will watch for new DOM elements using livequery, but also allows you to create DOM elements and use them as full objects with their own methods defined. https://github./hafriedlander/jquery.entwine

More info from jQuery's .on() docs

You can use Delegated events to create a click handler which will fire when an element is dynamically added to your original selector (typically a container), such as:

$( "#dataTable tbody" ).on( "click", "tr", function() {
  alert( $( this ).text() );
});

Now, when a new <tr> is added dynamically, it will have the click handler bound to it. However, there is no event for the actual loading of an element into the DOM.

Event handlers are bound only to the currently selected elements; they must exist on the page at the time your code makes the call to .on(). To ensure the elements are present and can be selected, perform event binding inside a document ready handler for elements that are in the HTML markup on the page. If new HTML is being injected into the page, select the elements and attach event handlers after the new HTML is placed into the page. Or, use delegated events to attach an event handler, as described next.

Delegated events have the advantage that they can process events from descendant elements that are added to the document at a later time. By picking an element that is guaranteed to be present at the time the delegated event handler is attached, you can use delegated events to avoid the need to frequently attach and remove event handlers. This element could be the container element of a view in a Model-View-Controller design, for example, or document if the event handler wants to monitor all bubbling events in the document. The document element is available in the head of the document before loading any other HTML, so it is safe to attach events there without waiting for the document to be ready.

Why do you need an event at all? If the form is being added dynamically then run what you need to at the time

var form = '<form class="contact-form"></form>';
$('body').append(form);
console.log("Hi there!");

This method is a shortcut for .on( "load", handler ).

The load event is sent to an element when it and all sub-elements have been pletely loaded. This event can be sent to any element associated with a URL: images, scripts, frames, iframes, and the window object.

For example, consider a page with a simple image:

<img src="book.png" alt="Book" id="book">

The event handler can be bound to the image:

$( "#book" ).load(function() {
  // Handler for .load() called.
});

As soon as the image has been loaded, the handler is called.

Now put that inside a ready handler

$( document ).ready(function() {
  // onload functions here
});
发布评论

评论列表(0)

  1. 暂无评论