Javascript 查找最后一次出现的文本
我有以下类型的html表格,由php动态生成:Javascript 查找最后一次出现的文本,javascript,jquery,Javascript,Jquery,我有以下类型的html表格,由php动态生成: <tr><td>Kiss the Girls</td><td>2016-01-01</td></tr> <tr><td>Kiss the Girls</td><td>2016-02-05</td></tr> <tr><td>Along Came a Spider</td>
<tr><td>Kiss the Girls</td><td>2016-01-01</td></tr>
<tr><td>Kiss the Girls</td><td>2016-02-05</td></tr>
<tr><td>Along Came a Spider</td><td>2016-01-07</td></tr>
<tr><td>Along Came a Spider</td><td>2016-01-22</td></tr>
<tr><td>Along Came a Spider</td><td>2016-03-31</td></tr>
我希望能够有一个动态显示过滤器,允许用户点击一个框,隐藏所有,但最新版本的手稿。所以它可能看起来像:
<tr><td>Kiss the Girls</td><td>2016-02-05</td></tr>
<tr><td>Along Came a Spider</td><td>2016-03-31</td></tr>
此时,没有一个或标记具有id或类,但我可以轻松地将类添加到第一列,例如。页面上只有一个表,php已经按日期对其进行了排序。我对jQuery或本机JavaScript持开放态度,尽管我认为使用jQuery会更容易。看起来在它改变名字之前抓取最后一行就可以了,但我不知道怎么做。有什么想法吗?从tr中获取对象后,迭代tr并存储在key-value对中,其中key作为td内容,value作为对象
变量a={};//用于存储dom元素对象的对象
$'table tr'.eachfunction{
a[$'td:first',this.text.trim]=this;//根据列更新dom元素对象
};
var$res=$$.mapa,functionv{
返回v;//获取对象并转换为jQuery对象
};
console.log$res;
$res.css‘颜色’、‘红色’;
吻那些女孩
2016-01-01
吻那些女孩
2016-02-05
来了一只蜘蛛
2016-01-07
来了一只蜘蛛
2016-01-22
来了一只蜘蛛
2016-03-31
根据“似乎只需在最后一行更改名称之前抓取它就可以完成”,这就是我得出的结论:
var rows = $("table tr");
if(rows.length > 0){
var last = $(rows[0]).find('td')[0].innerText;
for(var i=1; i<rows.length; i++){
var row = $(rows[i]);
var text = row.find('td')[0].innerText;
if(text === last){
$(rows[i-1]).hide();
}
last = text;
}
}
查看谭丽豪的笔。您可以反向迭代,并在运行过程中删除之前看到的所有内容: 函数过滤器先前版本{ var-seen={}; $$'tr.get.reverse.each函数{ var text=$'td',this.first.text; 如果看到[文本] $this.移除; 所见[文本]=真; } ; } 过滤器以前的版本; 吻那些女孩 2016-01-01 吻那些女孩 2016-02-05 来了一只蜘蛛 2016-01-07 来了一只蜘蛛 2016-01-22 来了一只蜘蛛 2016-03-31
如果在添加行时按递增顺序添加ID 你可以使用这个: var VALARY=[]; $'.maindiv'.eachfunction{ valArray.pushparseInt$this.attr'id',10; } valArray.sort函数a,b{ 返回a-b } alertLast row:+document.getElementByIdvalArray[valArray.length-1].innerHTML;//最高的` alertSecond last:+document.getElementByIdvalArray[valArray.length-2].innerHTML; 第2行的内容 第5行的内容 第3行的内容 第1行的内容 测试行中的内容
第4行的内容将其放在一起: 简洁:对于具有许多重复值的大型表,可能会有一些性能影响
$('tr').each(function(){
$("tr :contains('" + $('td', this).first().html() + "')").last()
.parent().css('color', 'red');
});
简明扼要的版本说明:-
$('tr').each(function(){ // for each row of the table
$("tr // find a child inside a tr
:contains('" // that contains the text
+ $('td', this) // present within a td of the row (in line 1)
.first().html() // at the beginning
+ "')") // Using string concat to pass variable to `contains` selector)
.last() // at the end (last occurence of text)
.parent() // invoke `parent()` to select whole row
.css('color', 'red'); // apply css to identify the desired row.
});
详细:使用ECMAScript 6或$.unique集合从完整名称列表中删除重复项。这样,当代码末尾的forEach循环运行时,每个名称只迭代一次
var uniqueNames = [];
$('tr').each(function(){
uniqueNames.push($('td', this).first().html());
}); // this will return the list of names from the table
// Remove duplicates from the list of names
uniqueNames = new Set(uniqueNames); // OR: uniqueNames = $.unique(uniqueNames);
uniqueNames.forEach(function(el){
$("tr :contains('" + el + "')").last().parent().css('color', 'red');
});
工作完美。。。特别是在我添加了一点if$this.is':checked“{…}else{$tr.show;}logicObject.keys直到第9版才在Internet Explorer中实现,请改用$.mapobj,func;花了一段时间才完全理解。但是,这是天才!简洁的美!!谢谢。@SarathChandra:很高兴能帮忙: