javascript jQuery选择具有相同ID的所有项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19583788/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:03:21  来源:igfitidea点击:

jQuery select all items with same ID

javascriptjqueryhtml

提问by pie6k

I know its bad, I know ID is unique and I need to fix it on massive scale on some set of pages.

我知道这很糟糕,我知道 ID 是唯一的,我需要在某些页面集上大规模修复它。

I dont know what are those ID, I only know class of it, so is it possible to somehow do

我不知道那些 ID 是什么,我只知道它的类别,所以有可能以某种方式做

$('.someClass').itemsThatHasIdDuplicate().each(function(){
    $(this).attr('id', Math.random()); //its stupid to use random for ID, but shows what I mean
});

ps. I've found this, but this assumes that you know what the ID is.

附:我找到了this,但这假设您知道 ID 是什么。

回答by pala?н

You can do this using the .attr( attributeName, function(index, attr) ):

您可以使用以下方法执行此操作.attr( attributeName, function(index, attr) )

// Get all the items with Duplicate id
var $itemsThatHasIdDuplicate = $('[id]').filter(function () {
    return $('[id="' + this.id + '"]').length > 1;
});

// Modify the id for all of them
$itemsThatHasIdDuplicate.attr('id', function (i, val) {
    return 'newID' + (i + 1);
});

Demo: Fiddle

演示:小提琴

回答by Milind Anantwar

First of all add some class to all the element having same id.

首先,为所有具有相同 id 的元素添加一些类。

$('[id]').each(function(){
  var ids = $('[id="'+this.id+'"]');
  if(ids.length>1 && ids[0]==this){
    $('#'+this.id).addClass('changeID');
}
});

and then change the id of all the element having that class...

然后更改所有具有该类的元素的 id...

$('.changeID').each(function(){
$(this).attr("id","changed_"+Math.random());
}

FYI: I would suggest you picking date time for assigning id rather than using math.random()

仅供参考:我建议您选择日期时间来分配 id 而不是使用 math.random()

回答by Felix Kling

What you can do is iterate over all elements of that class and group those elements with the same ID. Then modify those groups with more than one element:

您可以做的是迭代该类的所有元素,并使用相同的 ID 将这些元素分组。然后用多个元素修改那些组:

var IDs = {};   
var idModifier = function(index, id) {
    return id + index;
};

$('.someClass').each(function() {
    (IDs[this.id] || (IDs[this.id] = [])).push(this);
});

$.each(IDs, function(id, elements) {
    if (elements.length > 1) {
        $(elements).prop('id', idModifier);
    }
});

The "advantage" of this method is that you are searching the document only once, at the beginning with $('.someClass').

这种方法的“优点”是您只搜索文档一次,以$('.someClass').

回答by Sergiu Paraschiv

You have to make sure you don't overwrite the original ID

您必须确保不要覆盖原始 ID

var cls = 'foo';
$('.' + cls).each(function() {
    var me = this;
    var id = $(me).attr('id');
    var dupes = $('#' + id + '.' + cls);
    dupes.each(function() {
        if(this != me) {
            $(this).attr('id', id + '_r_' + Math.random());
        }
        else {
            $(this).attr('id', id + '_o');
        }
    });
});

This way you also know what the first instance of each ID is.

通过这种方式,您还知道每个 ID 的第一个实例是什么。

http://jsfiddle.net/g2DWR/1/

http://jsfiddle.net/g2DWR/1/

Edit: also, in a plugin formso you can chain it $('.foo').fixDupes().css('color', 'red');

编辑:另外,以插件形式,以便您可以链接它$('.foo').fixDupes().css('color', 'red');