[英]How can i get a unique id for every class items in this js
Here is a text, and when we add content through the textbox, then every new line adds into a new div class with quiz
in inner
div.这是一个文本,当我们通过文本框添加内容时,每一个新行都会添加到一个新的 div 类中,并在
inner
div 中进行quiz
。
Just Like This:像这样:
<div id="inner">
<div class="quiz">this is line one</div>
<div class="quiz">this is second line</div>
<div class="quiz">this is third line</div>
<div class="quiz">and so on...</div>
</div>
But I want to add a unique id in the "n" Number series from "1" on every quiz class Just Like this output I want when we add content through this textbox.但是我想在每个测验类的“1”的“n”数字系列中添加一个唯一的 id 就像当我们通过这个文本框添加内容时我想要的这个输出。 Like That:
像那样:
<div id="inner">
<div id="1" class="quiz">this is line one</div>
<div id="2" class="quiz">this is second line</div>
<div id="3" class="quiz">this is third line</div>
<div id="4" class="quiz">and so on...</div>
</div>
My Whole Code is:我的整个代码是:
const sendButton = document.getElementById('send-btn'); const textArea = document.getElementById('input'); const innerDiv = document.getElementById('inner'); var message = textArea.value; sendButton.addEventListener('click', function() { // split the textarea entries into an array let lines = (textArea.value).split("\\n"); // iterate over each line, creating a div/span and inserting into the DOM lines.forEach( (line) => { let encodedLine = encodeHtmlEntity(line); let newElement = `<div class="quiz">${encodedLine}</div>`; innerDiv.innerHTML += newElement; }); // reset the textarea textArea.value = ''; }); function encodeHtmlEntity(input) { var output = input.replace(/[\ -\香<>\\&]/gim, function(i) { return '&#' + i.charCodeAt(0) + ';'; }); return output; }
<div id="inner"> </div> <br> <textarea class="input" id="input" placeholder="Message..."></textarea><br> <button class="waves-effect waves-light" id="send-btn">Add Content</button>
What some new lines of code I need to write, anyone can help me.我需要写一些新的代码行,任何人都可以帮助我。
you can use something like counter to count IDs and attach it to your elements.您可以使用诸如 counter 之类的东西来计算 ID 并将其附加到您的元素上。 this is your code above with some changes:
这是你上面的代码,有一些变化:
let counter = 0;
lines.forEach( (line) => {
let encodedLine = encodeHtmlEntity(line);
let newElement = `<div id="${counter++}" class="quiz">${encodedLine}</div>`;
innerDiv.innerHTML += newElement;
});
let me know if it works for you.请让我知道这对你有没有用。
// ...
let id = 0; // *** Or whatever you want to start with
let index = 0; // In case you want to use an array of string values, instead, like this:
let ids = ['first-id', 'second-id', 'third-id', 'etc...']; // Must match lines
// iterate over each line, creating a div/span and inserting into the DOM
lines.forEach( (line) => {
// You can set id here
// id++; // Or you could increment by whatever you want
//
// id = id + 5; // Or you could use an array of arbitrary values
//
// id = list_of_ids[index++]; // increment index
//
// or...
let encodedLine = encodeHtmlEntity(line);
let newElement = `<div class="quiz" id="${id++}">${encodedLine}</div>`; // <-- *** Add id and increment
innerDiv.innerHTML += newElement;
});
// ...
Lines marked with "***" are really the only ones needed.标有“***”的行实际上是唯一需要的行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.