且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更换Ajax响应一个div的内部HTML

更新时间:2023-11-24 12:38:40

是在回调的窗口。使用给予回调的每个值:

  $(。时间)。每个(函数(指数,ELEM){
            VAR sendTime = $(本).attr(数据时间);
            dataString =sendtime =+ sendTime +与& Q = convertTime;
            $阿贾克斯({
                键入:POST,
                网址:data_handler.php
                数据:dataString,
                缓存:真正的,
                成功:函数(响应){
                    警报(响应);
                    $(ELEM)。html的(响应);
                }
            });
        });
 

您的的需要定义一个新的变量,以保护在jQuery已经会为你。

i am trying to change inner HTML of a div after some interval. i am getting right response which i want with Ajax. but unable to replace inner HTML of selected after and with Ajax response. what is wrong with my code..

Html

      <p class="time ui-li-desc" data-time="2013-02-13 11:30:08" >
    51 seconds ago<img alt="image" src="images/read.png"></p>

      <p class="time ui-li-desc" data-time="2013-02-13 11:30:16" >
    58 seconds ago<img alt="image" src="images/read.png"></p>
.
.
.
.
.
      <p class="time ui-li-desc" data-time="2013-02-13 11:40:08" >
    10 minute ago<img alt="image" src="images/read.png"></p>

j query

setInterval(function() { 
            $( ".time" ).each(function( index ) {
                var sendTime=  $(this).attr("data-time");
                dataString = "sendtime="+sendTime+"&q=convertTime";
                $.ajax({
                    type: "POST",
                    url: "data_handler.php",
                    data: dataString,                   
                    cache: true,
                    success: function(response) {
                        alert(response);
                        $(this).html(response);
                        //alert(response);
                    }
                });
            });
        }, 5000);

this is the window in the callback. Use the value given to the callback of each :

        $( ".time" ).each(function(index , elem) {
            var sendTime=  $(this).attr("data-time");
            dataString = "sendtime="+sendTime+"&q=convertTime";
            $.ajax({
                type: "POST",
                url: "data_handler.php",
                data: dataString,                   
                cache: true,
                success: function(response) {
                    alert(response);
                    $(elem).html(response);
                }
            });
        });

You don't need to define a new variable to protect this as jQuery already does it for you.