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");
                }
            }
         }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题