为什么与相同的字符串值相比,innerHTML不返回true? [英] why innerHTML does not return true when compared with same string value?

查看:86
本文介绍了为什么与相同的字符串值相比,innerHTML不返回true?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的html页面上有两个表,它们的数据完全相同,但是可能有一些差异需要突出显示. 我并使用下面的Javascript,但似乎innerHTML无法按预期工作-

I have two tables on my html page with exact same data but there may be few difference which need to be highlighted. I and using the below Javascript but seems innerHTML does not work as expected-

function CompareTables()
{
var table1 = document.getElementById("table1")
var table2 = document.getElementById("table2")



for(var i=1; i < table1.rows.length; i++)
{
    for(var j=1; j < table2.rows.length; j++){
        var tab1Val = table1.rows[i].cells[0].innerHTML;
        var tab2Val = table2.rows[j].cells[0].innerHTML;
        alert(tab1Val.toUpperCase()+"----"+tab2Val.toUpperCase());

        var changes =RowExists(table2,tab1Val);

        if(!changes[0])
        {
        table1.rows[i].style.backgroundColor = "red";
        instHasChange = true;
        }
}
 function RowExists(table,columnValue)
{
 var hasColumnOrChange = new Array(2);
 hasColumnOrChange[0] = false;
  for(var i=1; i < table.rows.length; i++)
 {

 if(table.rows[i].cells[0].innerHTML == columnValue) /*** why these two does not match**/
 {
   hasColumnOrChange[0] = true;
  }
 return hasColumnOrChange;
}
}

请在这里提出问题. (table.rows[i].cells[0].innerHTML == columnValue)永远不会返回true,即使所有值都相同.

Please suggest what wrong here. (table.rows[i].cells[0].innerHTML == columnValue) never returns true even if all values same.

推荐答案

尝试并使用 Jquery的方法.text

取决于浏览器(Firefox和Chrome的)innerHTML不起作用

Depending on the browser(Firefox and Chrome's) innerHTML does not work

JQuery为您解决了这个问题.

JQuery takes care of that issue for you.

这篇关于为什么与相同的字符串值相比,innerHTML不返回true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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