简体   繁体   English

如何使用javascript而不是jQuery提供唯一的html类名称

[英]How to give unique html class name using javascript not from jquery

I am new in JavaScript, I want to search the number of id=heading and append unique class name to the existing class. 我是JavaScript的新手,我想搜索id = heading的数量并将唯一的类名附加到现有的类中。

I have tried to do this using JavaScript but able to add only to the first heading only. 我尝试使用JavaScript进行此操作,但只能将其仅添加到第一个标题中。 Please refer the code-pen: https://codepen.io/restar312/pen/EqWOyV 请参阅代码笔: https//codepen.io/restar312/pen/EqWOyV

var check_div = document.getElementsByTagName('div');
var numheading = 0;
for (var i = 0; i < check_div.length; i++) {
  if (check_div[i].id.indexOf('heading') != -1)
    numheading++;
  console.log(numheading);
  var d = document.getElementById("heading");
  d.className += numheading;
}

Expected class name should be class=First_heading1...class=First_heading2...class=First_heading3 期望的类名称应为class = First_heading1 ... class = First_heading2 ... class = First_heading3

The problem is that your heading have the same identical id heading . 问题是您的标题具有相同的id heading You should use different ids to access different DOM elements. 您应该使用不同的ID来访问不同的DOM元素。

For example: 例如:

<div id="heading1" class=First_heading>
  <h1>Heading1</h1>
</div>
<div id="heading2" class=First_heading>
  <h1>Heading2</h1>
</div>
<div id="heading3" class=First_heading>
  <h1>Heading3</h1>
</div>

document.getElementById("heading") document.getElementById(“ heading”)

Will always provide you with the first element having that id . 将始终为您提供具有该id的第一个元素。 Document.getElementById() Document.getElementById()

className s do not require to be unique, yet id s do. className不需要唯一,而id必须唯一。 So you could loop through all div with that className and assign those an unique id instead. 因此,您可以使用该className遍历所有div并为其分配唯一的id

 window.onload = function(){ for(var tListOfElements=document.querySelectorAll('.heading'), i=0, j=tListOfElements.length; i<j; i++){ tListOfElements[i].id = tListOfElements[i].id || 'heading_' + i; tListOfElements[i].textContent = tListOfElements[i].id } }; 
 <div class = 'heading'>A</div> <div class = 'heading'>B</div> <div class = 'noheading'>C</div> <div class = 'heading'>D</div> <div class = 'heading' id = 'wannakeepmyid'>E</div> 

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

相关问题 如何使用具有唯一类名的jquery定位链接? - How to target a link using jquery that has a unique class name? 如何使用javascript给特定的td一个类名? - how to give a certain td a class name with javascript? 如何使用 Javascript 或 JQuery 替换 HTML 标签,而不使用任何 Class 名称或 - How to replace HTML Tags using Javascript or JQuery without any Class name or ID 如何通过使用javascript(不允许使用jQuery)获取类名来更改HTML样式? - How to change HTML style by taking the class name using javascript (no jQuery allowed)? 使用jQuery为每个第一,第二和第三个元素提供一个唯一的类 - Give every first, second and third element a unique class using jQuery 如何在javascript中为html标记id提供唯一的id - How to give html tag id a unique id in javascript 如何使用javascript或jquery从具有相同CSS类名称的两个不同下拉列表中选择选项 - How to select options from two different dropdowns of same css class name using javascript or jquery 如何使用 JavaScript 切换或更改 HTML class 名称 - how to toggle or change HTML class name using JavaScript 如何为使用jquery动态生成的按钮赋予唯一ID - how to give unique id to buttons generated dynamically using jquery 如何使用javascript为选定的文本赋予唯一的ID? - How to give the SELECTED text an unique ID using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM