6

我正在尝试在一段时间后更改 div 的内部 HTML。我得到了我想要的 Ajax 的正确响应。但无法用 Ajax 响应替换选定的内部 HTML。我的代码有什么问题..

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 查询

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);
4

4 回答 4

9

this是回调中的窗口。使用赋予callbackeach 的值:

        $( ".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);
                }
            });
        });

不需要定义一个新的变量来保护this,因为 jQuery 已经为你做了。

于 2013-02-13T10:46:38.920 回答
5

当您使用带有回调的异步函数时,this您的回调不是来自相同的上下文。您需要保存this在回调中使用的变量中。

试试这样:

setInterval(function() { 
            $( ".time" ).each(function( index ) {
                var sendTime=  $(this).attr("data-time");
                dataString = "sendtime="+sendTime+"&q=convertTime";
                var self = this;
                $.ajax({
                    type: "POST",
                    url: "data_handler.php",
                    data: dataString,                   
                    cache: true,
                    success: function(response) {
                        alert(response);
                        $(self).html(response);
                        //alert(response);
                    }
                });
            });
        }, 5000);
于 2013-02-13T10:46:48.857 回答
1

我认为 $(this) 是断章取义的。尝试:

setInterval(function() { 
            $( ".time" ).each(function( index ) {
                var $this = $(this);
                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);
于 2013-02-13T10:47:17.093 回答
1
setInterval(function() { 
        $( ".time" ).each(function( index ) {
            var sendTime=  $(this).attr("data-time");
            var _thisvariable = $(this);
            dataString = "sendtime="+sendTime+"&q=convertTime";
            $.ajax({
                type: "POST",
                url: "data_handler.php",
                data: dataString,                   
                cache: true,
                success: function(response) {
                    alert(response);
                    _thisvariable.html(response);
                    //alert(response);
                }
            });
        });
    }, 5000);
于 2013-02-13T10:47:48.840 回答