如何比较几个td.text()在tr使用jQuery [英] How to compare several td.text() in tr using jQuery

查看:219
本文介绍了如何比较几个td.text()在tr使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<table>
<tr id="tr1">
<td id="td1"> test1 </td>
<td id="td2"> test2 </td>
<td id="td3"> test1 </td>
<td id="td4"> test3 </td>
</tr> 
</table>

这里我有一个包含 tr 在它和4 td 的。
现在,我的问题是,我如何比较 td.text()与另一个?

Here I have a table with a tr in it and 4 td's. Now, my question is, how can I compare the td.text() with the other one?

例如:
a循环接受第一个 td 的文本,然后与其他 td 的。
如果相同,则给予 td 一个类。

For example: a loop that takes the text of first td and then compare it with other td's. If it is the same, then give that td a class.

td id =td1应该得到一个类

>
当我在3e td 时,3e td 应该得到一个类。 p>

BUT: When I'm at the 3e td, the 3e td should get a class.

推荐答案

此代码适用于您:

var tds;
$('tr').each(function(i, item){
    tds = $(this).find('td');
    tds.each(function(j, elem1){
        tds.each(function(k, elem2){
            if($(elem1)[0] != $(elem2)[0] && $(elem1).text() == $(elem2).text()){
                $(elem1).addClass('cl');
            }
        });
    });
});

FIDDLE: https://jsfiddle.net/lmgonzalves/cqa6m6va/1/

这篇关于如何比较几个td.text()在tr使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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