jQuery:查找重复的 ID 并删除除第一个之外的所有 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15759882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: Finding duplicate ID's and removing all but the first
提问by ditto
$('[id]').each(function () {
var ids = $('[id="' + this.id + '"]');
// remove duplicate IDs
if (ids.length > 1 && ids[0] == this) $('#' + this.id).remove();
});
The above will remove the first duplicate ID, however I want to remove the last. I've tried $('#'+ this.id + ':last')
but to no avail.
以上将删除第一个重复的 ID,但是我想删除最后一个。我试过了,$('#'+ this.id + ':last')
但无济于事。
In the fiddle the input with the value 'sample' should be kept when the append action takes place.
在小提琴中,当发生追加操作时,应保留值为 'sample' 的输入。
回答by novalagung
Use jquery filter :gt(0)
to exclude first element.
使用 jquery 过滤器:gt(0)
排除第一个元素。
$('[id]').each(function () {
$('[id="' + this.id + '"]:gt(0)').remove();
});
Or select all the available elements, then exclude the first element using .slice(1)
.
或者选择所有可用元素,然后使用 排除第一个元素.slice(1)
。
$('[id]').each(function (i) {
$('[id="' + this.id + '"]').slice(1).remove();
});
回答by Mahesh Reddy
Try:
尝试:
$('[id="' + this.id + '"]:not(#" + this.id + ":first)').remove();
回答by Mahesh Reddy
you can try
你可以试试
$("#ID").nextAll().remove();
回答by user2254898
$('[id]').each(function() {
var $ids = $('[id=' + this.id + ']');
if ($ids.length > 1) {
if(this.id === your_id)//which is duplicating
$ids.not(':first').remove();
}
});
回答by Alnitak
This code is longer than some of the others, but the double-nested loop should make its operation obvious.
这段代码比其他一些代码要长,但双嵌套循环应该使它的操作显而易见。
The advantage of this approach is that it only has to scan the DOM to generate the list of elements with an id
attribute once, and then uses the same list to find (and remove) the duplicates.
这种方法的优点是它只需要扫描 DOM 以生成具有id
属性的元素列表一次,然后使用相同的列表来查找(并删除)重复项。
Elements that were already removed will have parentNode === null
so can be skipped while iterating over the array.
已经被删除的元素将parentNode === null
因此可以在迭代数组时被跳过。
var $elems = $('[id]');
var n = $elems.length;
for (var i = 0; i < n; ++i) {
var el = $elems[i];
if (el.parentNode) { // ignore elements that aren't in the DOM any more
var id = el.id;
for (var j = i + 1; j < n; ++j) {
var cmp = $elems[j];
if (cmp.parentNode && (cmp.id === id)) {
$(cmp).remove(); // use jQuery to ensure data/events are unbound
}
}
}
}
回答by rab
try this
尝试这个
var duplicated = {};
$('[id]').each(function () {
var ids = $('[id="' + this.id + '"]');
if ( ids.length <= 1 ) return ;
if ( !duplicated[ this.id ] ){
duplicated[ this.id ] = [];
}
duplicated[ this.id ].push( this );
});
// remove duplicate last ID, for elems > 1
for ( var i in duplicated){
if ( duplicated.hasOwnProperty(i) ){
$( duplicated[i].pop() ).remove();
}
}
and jsfiddle is http://jsfiddle.net/z4VYw/5/
和 jsfiddle 是http://jsfiddle.net/z4VYw/5/
回答by Vignesh Ben
Try Using The below function Works fine for me:
尝试使用以下功能对我来说很好用:
$('#favourities-ajax ul li').each(function(i) {
$('[id="' + this.id + '"]').slice(1).remove();
});
回答by Jon Egerton
I'd use not()
to remove the current element from ids
and remove the rest:
我会not()
用来删除当前元素ids
并删除其余元素:
(Fiddle)
(小提琴)
$('body').on("click", ".placeholder", function() {
data = '<section id="e1"><input name="e1name" /></section><section id="two"><input name="two" value="two section" /></section>';
$('form').append(data);
// ideally I'd like to run this function after append but after googling I find that's not possible.
// for each ID ...
$('[id]').each(function () {
var ids = $('[id="' + this.id + '"]');
if (ids.length>1) {
ids.not(this).remove();
}
return;
});
});