简体   繁体   English

我怎样才能为这个 js 中的每个类项目获得一个唯一的 id

[英]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.

相关问题 我如何获得数组React js中的第一个唯一ID元素和最后一个唯一ID元素 - how can I get the first unique ID element and last unique ID element in an array React js 如何以角度获取每个设备的唯一ID - how to get unique id of every device in angular 如何在表中设置唯一 ID? JS - How can i set unique ID in table? JS 如何获得每个访问者的唯一 ID? php - How can I get a unique Id of each visitor? php 如何在 javascript 中获得唯一的设备 ID? - how can I get a unique device ID in javascript? 我如何获取ID到CasperJS中页面上的每个复选框 - How can I get the id to every checkbox on a page in CasperJS 我将输入类型的 id 作为唯一 id,那么我怎样才能将这些唯一 id 放入 javascript 中? - i am having id of input type as unique id so how can i get those unique id into the javascript? JavaScript:如何用包含唯一ID的span标签将每个单词包装在字符串中? - JavaScript: How can I wrap every word in a string with a span tag containing a unique id? 如何让我的 JS 函数忽略 ID CSS 而只查看 Class? - How can I get my JS function to overlook ID CSS and only look at Class? 每次选择更改时,如何检索collection_select的ID并显示项目列表? - How can I retrieve the ID of a collection_select and display a list of items every time the select changes?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM