jQuery突出显示了表中的差异

jQuery highlight differences in tables

本文关键字:显示 jQuery      更新时间:2023-09-26

我正在尝试比较两个HTML表,这两个表在概念上与这里所做的类似:逐行比较两个html表的数据,并使用jquery突出显示

除了我只需要知道其中是否有任何数据不同时出现在两者中。

到目前为止,我的解决方案是浏览第一张表,如果我在第二张表中找到匹配项,请突出显示。如果没有匹配项,则在第一张表中突出显示。(使用已找到和未找到的类)。

这是我的密码。选择器正在工作,但类似乎没有坚持。

$(document).ready(function(){
    $("#oldScript tr").each(function(){
        $('#newScript tr:contains('+ "" + this.innerHTML + "" + ')').addClass("found");
        if($('#newScript tr:contains('+ "" + this.innerHTML + "" + ')').length == 0){
            $(this).addClass("notFound");
            alert("Row not found 'n" + $(this).innerHTML);
        }
    });
});

使用each时,最好使用jQuery Docs中定义的回调函数中的参数。有一个fiddle会很有帮助,但可以尝试这样的方法,而不是在循环中使用this(现在注意回调函数的参数)。

$(document).ready(function(){
    $("#oldScript tr").each(function(index, item) {
        $('#newScript tr:contains('+ "" + item.innerHTML + "" + ')').addClass("found");
        if($('#newScript tr:contains('+ "" + item.innerHTML + "" + ')').length == 0){
            $(item).addClass("notFound");
            alert("Row not found 'n" + item.innerHTML);
        }
    });
});