jQuery:计算表列中的特定字符 [英] jQuery: Count specific character in a table column
问题描述
我有一个HTML表,每行有两列。第一列是大写字母,第二列是品牌名称,如下所示:
I have a HTML table where each row has two columns. First column is a capital letter and the second one is a brand name, like so:
A |亚马逊
A | Apple
A | Apple
B |宝马
C |香奈儿
等。但是,只要有两个品牌名称具有相同的首字母大写字母,我希望该表格如下所示:
etc. But whenever there are two brand names that has the same first capital letter I would like the table to look like this:
A |亚马逊
| Apple
| Apple
B |宝马
C |香奈儿
换句话说,如果每个大写字母都有多个实例,我只想显示第一个。如果我将一个类应用到第一列,是否有一种方法可以使用jQuery实现这一点?
In other words, if there are more than one instance of each capital letter I would like to display only the first one. If I applied a class to the first column, is there a way I could achieve this using jQuery?
推荐答案
你可以这样做使用 each()函数(假设您的第一列的类是 leftCol
):
You can do that with the each() function (assuming the class of your first column is leftCol
):
$(document).ready(function() {
var lastLetter = "";
$(".leftCol").each(function() {
var $this = $(this);
var text = $this.text();
if (text != lastLetter) {
lastLetter = text;
} else {
$this.text("");
}
});
});
这篇关于jQuery:计算表列中的特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!