比较JavaScript中的HTML表格单元格内容

时间:2010-03-23 10:29:31

标签: javascript jquery

<table>
    <tr>
        <td class = "nowrap cr" id="cr1">123123</td>
        <td class = "nowrap ch" id="ch1">123123</td>
    </tr>
    <tr>
        <td class = "nowrap cr" id="cr2">123123</td>
        <td class = "nowrap ch" id="ch2">123123</td>
    </tr>
    <tr>
        <td class = "nowrap cr" id="cr3">467574</td>
        <td class = "nowrap ch" id="ch3">123123</td>
    </tr>
</table>

如何在font-weight: bold tr

中设置chID == crID

我已经编写了如下的JS代码

$(function () {
    for (var i = 0; i < 20; i++)
    {
        if ($('#cr' + i).val() == $('#ch' + i).val())
        {
            $('#cr' + i).parent().css('font-weight', 'bold');
        }
    }
});

但它不起作用。

让我知道如何实现所需的输出?

1 个答案:

答案 0 :(得分:2)

val()仅用于输入元素。你需要使用

if($('#cr'+i).html() == $('#ch'+i).html())

或者,仅比较文本值(可能是你想要的)

if($('#cr'+i).text() == $('#ch'+i).text())