简体   繁体   English

在动态div上使用jQuery的AddID

[英]AddID with jQuery on a dynamic div

I'm trying to add an id to each div that it generates something like this. 我正在尝试为每个div添加一个ID,它会生成这样的内容。

<div class="wpmlposts"></div>
<div class="wpmlposts"></div>
<div class="wpmlposts"></div>
<div class="wpmlposts"></div>
<div class="wpmlposts"></div>

How can I add an id to each of them dynamically? 如何为每个对象动态添加ID?

This is my code 这是我的代码

var sizestring= $('.wpmlposts').length; 
for (i=0;i <sizestring;i++){
}

I know its incomplete, but I have no idea how can I add an id to each div! 我知道它不完整,但是我不知道如何为每个div添加一个ID!

You can use attr() or prop() method with a callback function which iterates the elements internally and generates the id value using first arguments in the callback which is the index of the element in the collection. 您可以将attr()prop()方法与回调函数一起使用,该函数在内部对元素进行迭代并使用回调中的第一个参数(即集合中元素的索引)生成id值。

 $('.wpmlposts').attr('id', function(i) { return 'ranId' + i; }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> 

To add an id is easy, assuming that those elements are present in the DOM already: 假设这些元素已经存在于DOM中,添加一个id很容易:

$('.wpmlposts').prop('id', function(i){
    return 'idPrefix' + i;
});

 $('.wpmlposts').prop('id', function (i){ return 'idPrefix' + i; }); 
 div::before { content: attr(id); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> 

Incidentally, you really don't need JavaScript for this, the following would work just as well (in relatively up-to-date browsers): 顺便说一句,您确实不需要JavaScript,以下代码也可以工作(在相对最新的浏览器中):

Array.from(document.querySelectorAll('.wpmlposts')).forEach((el,index) => el.id = 'idPrefix' + i);

 Array.from(document.querySelectorAll('.wpmlposts')).forEach((el,index) => el.id = 'idPrefix' + index); 
 div::before { content: attr(id); } 
 <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> 

References: 参考文献:

you can also do it like this 你也可以这样

 $('.wpmlposts').each(function(i){ $(this).attr('id','randId'+i); }); // displaying here $('.wpmlposts').each(function(i){ console.log(this); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> 

You need to use the each function to treat every node individually. 您需要使用each函数分别处理每个节点。 each function 每个功能

$('.wpmlposts').each(function(index) {$(this).attr('id', index);
});

 var _=$('.wpmlposts'); //let's cache the array var length = _.length; //let's cache the length for(var x=0; x<length; x++) { _.eq(x).prop('id', x); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> <div class="wpmlposts"></div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM