简体   繁体   中英

Click event is not working on Ajax loaded content

I am loading content using ajax from external HTML Files. Dont know why after loading content, click event is not working in safari (mobile safari as well) on any of the newly loaded elements (ul, li, images etc). However this is working in mozilla.

I am not able to understand the exact issue behind this. Please advice me the solution. Below is the code for reference.

Note: I am using the below code under jquery ready function. Is jquery is the cause of issue??

var currentBottle = this.title; var request = createRequest(); if (request == null) { alert("Unable to create request"); return; } request.onreadystatechange = showContent; request.open("GET", currentBottle + ".html", true); request.send(null);

function showContent() { if (request.readyState == 4) { if (request.status == 200) { document.getElementById("food_scroller").innerHTML = request.responseText; } } }

Event binding breaks down when the content of a page loaded through ajax. You have to bind the events again in the document using the below procedure.

do this

$(document).on('click', '.classname' ,function (e) {
    // write your logic
});

instead of

$('.classname').on('click' ,function (e) {
    // write your logic
});

In Safari the content-type can sometimes matter, make sure the response type is set as text/html . Also in your AJAX loaded content you should try not to have <script> tags I don't think Safari respects those sometimes.

Maybe try to use jQuery's $.load() to GET HTML content cross-browser compatible (below is equivalent to your createRequest and showContent functions):

var currentBottle = this.title;
$.load(currentBottle + ".html",
    function(responseText,textStatus,XMLHttpRequest){
        $("#food_scroller").html(responseText);
        //bind you on click events here
        $("#food_scroller").find("ul, li, images").click(myClickFunction);
  }
);

I have been through this issue myself. Newly loaded html via Ajax needs to be connected to the event handlers, even if it is replacing existing DOM elements. To do that you have to include in your AJAX result to establish a handler for all event-sensitive elements that are being rendered.

So for example, if your AJAX call returns

$ajaxresult = '<button class="bookbtn" id="bookbtn" type="button" >Book</button>';

You need to add

$ajaxresult .= '<script>$("#bookbtn").on("click",function(){doit($(this))});</script>'."\n";

However in researching this issue just now, I read the following in http://api.jquery.com/on/ :

Event handlers are bound only to the currently selected elements; they must exist at the time your code makes the call to .on(). To ensure the elements are present and can be selected, place scripts after the elements in the HTML markup or perform event binding inside a document ready handler. Alternatively, use delegated events to attach event handlers.

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.

In addition to their ability to handle events on descendant elements not yet created, another advantage of delegated events is their potential for much lower overhead when many elements must be monitored. On a data table with 1,000 rows in its tbody, this example attaches a handler to 1,000 elements: ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM