[英]jQuery: highlight TDs with same value by click on another TD
I have a large HTML table with onclick
events on each td
(with the class myClass
). 我有一个很大的HTML表,每个
td
上都有onclick
事件(类为myClass
)。
Is there a way to make it so that by clicking on a TD I change the background colour for all td
s with the same value, or to highlight them in any other way? 有没有一种方法可以通过单击TD来更改所有具有相同值的
td
的背景颜色,或者以其他任何方式突出显示它们?
What I am looking for is a quick way to show td
s with the same value without the need to filter the table. 我正在寻找的是一种快速显示
td
且具有相同值而无需过滤表的方法。
Example TRs: TR示例:
<tbody>
<tr>
<td class="myClass">Value 1</td>
<td class="myClass">Value 5</td>
<td class="myClass">Value 3</td>
<td class="myClass">Value 1</td>
<td class="myClass">Value 2</td>
</tr>
<tr>
<td class="myClass">Value 3</td>
<td class="myClass">Value 2</td>
<td class="myClass">Value 1</td>
<td class="myClass">Value 5</td>
<td class="myClass">Value 2</td>
</tr>
</tbody>
This will make it so that whenever you click on a td
, it will highlight all the td
s with the same text yellow. 这样一来,每当您单击
td
,它将以黄色相同的文本高亮显示所有td
。
$("td").click(function() {
var value = $(this).text();
$("td")
.css("background-color", "")
.filter(function(){
return $(this).text() === value;
})
.css("background-color", "yellow");
});
You can see this in action on jsFiddle . 您可以在jsFiddle上看到这一点 。
Here is a straight up jquery answer that is brute force but I think goes along the lines of what you want 这是一个直截了当的jquery答案,是蛮力的,但我认为符合您想要的路线
$('.myClass').on('click',function(choice){
var choiceText = $(choice).text();
$('.myClass').each(function(index, element){
if($(element).text() === choiceText){
//do whatever you need
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.