0

jQuery 的克隆功能在 jsfiddle 中有效,但不适用于 CGI 生成的页面。

http://jsfiddle.net/FkrBc/1/

相关代码:

var displayDiff = $("#firstTable tbody").clone();

function checkee () {
    $( "tr", displayDiff ).each( function() {
        var foo = $(this).find("td:first-child").text();
        $("#firstTable tbody tr td:first-child").filter(function() {
        return $(this).text() == foo;
        }).parent().attr("class","same");

    });
    var content = $('#firstTable tbody>tr[class!="same"]').clone();
    $("#results").html(content);
}
rowCount = $("tr", displayDiff).length;

setInterval(function(){ 
  if($("#firstTable tr").length > rowCount){
     checkee();
  } 
}, 2000);

在小提琴中,它只克隆一次并使用新条目更新该部分,在 CGI 页面上,它看起来像是在不断克隆。

当我通过将克隆输出到 div 进行测试时——它会不断更新表中的新行。不应该这样做。

有什么解决办法吗?

4

1 回答 1

0

您的更新 rowCount 不应该在 checkee() 函数内吗?像这样:

var rowCount = 0; //declare outside of function to maintain scope
function checkee () {
    $( "tr", displayDiff ).each( function() {
        var foo = $(this).find("td:first-child").text();
        $("#firstTable tbody tr td:first-child").filter(function() {
        return $(this).text() == foo;
        }).parent().attr("class","same");

    });
    var content = $('#firstTable tbody>tr[class!="same"]').clone();
    $("#results").html(content);
    rowCount = $("tr", displayDiff).length;  //this line moved to inside this function
}

现在 rowCount 只被设置一次,因为它在一个独立的行上,所以检查

$("#firstTable tr").length > rowCount)

setInterval 的函数内部始终为真(假设第一次为真)。

于 2012-10-24T03:46:01.827 回答