English 中文(简体)
JQuery, AJAX, PHP, XML; Image overlay script stops working on callback content
原标题:

A button click fires my function that fetches image data via an AJAX-call:

$("#toggle_album").click(function () {
   album_id = $("#album_id").val();
                $.post( backend/load_album_thumbnails.php , {
                 id: album_id
   }, function(xml) {
    var status = $(xml).find("status").text();
       var timestamp = $(xml).find("time").text();
    $("#album_thumbs_data_"+album_id+"").empty();
    if (status == 1) {
     var temp =   ;
     var output =   ;
     $(xml).find("image").each(function(){
      var url = $(this).find("url").text();
      temp = "<DIV ID="thumbnail_image"><A HREF="javascript:void(null);" CLASS="overlay">[img-tag with class="faded" goes here]</A></DIV>";
      output += temp;
     });
     $("#album_thumbs_data_"+album_id+"").append(output);
    } else {
     var reason = $(xml).find("reason").text();
     var output = "<DIV CLASS="bread">"+reason+"</DIV>";
     $("#album_thumbs_data_"+album_id+"").append(output);
    }
    $("#album_thumbs_"+album_id+"").toggle();
   });
  });

The data is returned in XML format, and it parses well, appending the data to an empty container and showing it;

My problem is that my image overlay script:

  $("img.faded").hover(   
  function() {   
   $(this).animate({"opacity": "1"}, "fast");
  },   
  function() {   
   $(this).animate({"opacity": ".5"}, "fast");   
  }); 

... stops working on the image data that I fetch via the AJAX-call. It works well on all other images already loaded by "normal" means. Does the script need to be adjusted in some way to work on data added later?

I hope my question is clear enough.

最佳回答

Okay, apparantly I hadn t googled it enough. Surfing my own question here on stackoverflow pointed me to other questions, which pointed me to the JQuery live() function: live().

However, it does not work on hover(), so I rewrote the script to use mouseover() and mouseout() instead:

    $("img.faded").live("mouseover",function() {
        $(this).animate({"opacity": "1"}, "fast");
    });
    $("img.faded").live("mouseout", function() {
        $(this).animate({"opacity": "0.5"}, "fast");
    });

... and now it works flawlessly even on the content I fetch from the AJAX-call.

Sorry if anyone has started writing an answer already.

问题回答

You have to bind the new events each time you add a DOM element to the page.

There is a built-in function in jquery called live that does that for you.

I noticed you add the images from your xml; you can add there the new binds too.

$(xml).find("image").each(function(){
    //this actually creates a jquery element that you can work with
    $( my-img-code-from-xml-goes-here ).hover(   
      function() {   
        $(this).animate({"opacity": "1"}, "fast");
      },   
      function() {   
        $(this).animate({"opacity": ".5"}, "fast");   
      }
        //i did all my dirty stuff with it, let s add it where it belongs!
    ).appendTo($( some-already-created-element ));
});

EDIT: corrected a wrong sentence.





相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签