更换Ajax响应一个div的内部HTML [英] Replace inner HTML of a div with Ajax response

查看:150
本文介绍了更换Ajax响应一个div的内部HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一些时间后改变一个div的内部HTML。 我得到它,我想与阿贾克斯正确的反应。 但无法取代内部HTML后,用Ajax响应选择。 什么是错我的code ..

HTML

 &其中,P类=时间UI丽递减数据时间为2013年2月13日11点30分08秒>
    51秒前< IMG ALT =形象SRC =图像/ read.png>< / P>

      &其中,P类=时间UI丽递减数据时间为2013年2月13日十一点30分16秒>
    58秒前< IMG ALT =形象SRC =图像/ read.png>< / P>
。
。
。
。
。
      &其中,P类=时间UI丽递减数据时间为2013年2月13日11时40分零八秒>
    10分钟前< IMG ALT =形象SRC =图像/ read.png>< / P>
 

Ĵ查询

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

解决方案

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

  $(。时间)。每个(函数(指数,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.

这篇关于更换Ajax响应一个div的内部HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆