我的HTML中有一张表格,
<table>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>a</td><td>2</td><td>c</td></tr>
<tr><td>x</td><td>2</td><td>3</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
</table>
我想在第一和第二个<td>
元素的内容中找到重复的内容。
这是我到目前为止所得到的:
$(function() {
$("table tr").each(function() {
});
});
我坚持选择第一个和第二个元素并进行比较。我只是添加一个类还是使用jquery选择器?
只需将前两个值的组合存储为对象中的属性
$(function() {
var el = {};
$("table tr").each(function() {
// get row
var row = $(this);
// get first and second td
var first = row.find('td:first-child').val();
var second = row.find('td:nth-child(2)').val();
// if exists, remove the tr
if(el[first + second]) {
$(this).remove())
}
else {
// if it does not exist, add it with some random val
el[first + second] = 1;
}
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句