Jquery在ajax加载的内容上选择最后一个

Jquery在ajax加载的内容上选择最后一个,jquery,Jquery,我试图在jquery中选择引导井的最后一个ID 这是可行的,但在ajax加载的油井上不行,请参阅下面我的尝试,有什么建议吗 var newest = $('.well:last').attr('id'); setInterval(function() { console.log(newest); $.ajax({ type: 'POST', url: 'ajax/getNew.php', data: "id="+newest+"&session=<?

我试图在jquery中选择引导井的最后一个ID

这是可行的,但在ajax加载的油井上不行,请参阅下面我的尝试,有什么建议吗

var newest = $('.well:last').attr('id');
setInterval(function() {
console.log(newest);
    $.ajax({
    type: 'POST',
    url: 'ajax/getNew.php',
    data: "id="+newest+"&session=<?php echo $sesh; ?>",
    cache: false,
    success: function(html) {
        $("#convo").append(html);
        newest = $(html).filter('.well:last').attr('id');
    }
    });
}, 3000);

通过成功的ajax结果添加新井后,var最新未定义。

可能没有根元素。这就是它不起作用的原因。 您可以尝试以下代码和选择器:

rootedHtml = "<div>"+html+"</div>";
newest = $(rootedHtml).find('.well[id]:last');
var id = newest.attr(id);
请检查此小提琴:

试试:

newest = $("#convo").find('.well:last').prop('id');
或:


取决于html的值。也许你需要。查找而不是。筛选。但是不需要对HTML进行两次解析,只需执行$conva.find'.well'.last.attr'id'。假设conva是页面上的一个div,并且假设HTML包含一个新的.well元素,我认为应该尝试调用$'.well:last'.attr'id';在成功处理程序中,为什么要使用过滤器?这里$html调用有效吗。。?或者应该使用$conva.find'.well:last'更好。。。有人吗?“最新的=$”.嗯:最后一个“.attr'id”;成功处理程序内部未定义“$conva.find”。well:last返回[div27.well-well-small,prevObject:e.fn.e.init[1],context:document,selector:conva.well:last,constructor:function,init:function…],而不是id
newest = $("#convo").find('.well').last().prop('id');