weixin_33705053 2016-08-17 06:41 采纳率: 0%
浏览 38

jQuery onChange div内容

I would like to run a function when a div content(an image) is charged, I have been trying this with html().length and onChange, but onChange only works when page is loaded, thanks:

My code:

if (($("#zv1").html().length > 0) && ($("#zv1").change())){
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }
        if (($("#zv2").html().length > 0) ) {
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }
        if (($("#zv3").html().length > 0) && ($("#zv3").change())) {
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }
        if (($("#zv4").html().length > 0) && ($("#zv4").change())) {
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }
        if (($("#zv5").html().length > 0) && ($("#zv5").change())) {
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }
        if (($("#zv6").html().length > 0) && ($("#zv6").change())) {
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }
        if (($("#zv7").html().length > 0) && ($("#zv7").change())) {
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }
        if (($("#zv8").html().length > 0) && ($("#zv8").change())) {
            //ion.sound.play("water_droplet_2");
            alert("sound");
        }

AJAX example:

function sync_vitrasa(id){
    $.ajax({
     url: "/vitrasa_state/",
     type:"GET",
     data: {
       id: id,
     },success: function( data ) {
        id="#zv"+id;
        if($(id).html().length == 0){
            $(id).html(data)
            alert("sound");
        }
        else {
            if($(id).html() != data) {

                //ion.sound.play("water_droplet_2");
                alert("image is already there");
            }
        }
     }
});
}

Check if submit in another HTML is clicked:

$(document).ready(function() {
          setInterval(function () {
                $.ajax({
                    url : '/vitrasa/',
                    success : function(data){
                        $('#sub2').click(function() {
                            alert( "calling Ajax." );
                            sync_vitrasa(2);
                        });
                    }
                });
            }, 500);

            });

PD: zvX is the div id for the HTML td that contais the image charged with AJAX

  • 写回答

1条回答 默认 最新

  • weixin_33694172 2016-08-17 06:45
    关注

    Why track the td html changes when you know it's being changed through AJAX?

    For instance, if you have something in your ajax response that says

    $('#zv8').html(result);
    

    Call whatever you need to change from there. You already know what's changing.

    Edit: Change to your ajax code

    I think something like this should get you what you want. This basically says if the element is empty put in the image. If it's not empty check if what I'm about to put in is different than what's in there. If it is, do something.

    function sync_vitrasa(id){
        $.ajax({
         url: "/vitrasa_state/",
         type:"GET",
         data: {
           id: id,
         },success: function( data ) {
            id="#zv"+id;
            if($(id).html(length) == 0){
                $(id).html(data)
            {
            else {
                if($(id).html != data) {
                    $(id).html(data);
                    //ion.sound.play("water_droplet_2");
                    alert("sound");
                }
            }
         }
    });
    
    评论

报告相同问题?