简体   繁体   English

使用JavaScript更改具有相同名称的多个类?

[英]Change Multiple Classes with the same name using JavaScript?

Check out my small project on codepen below... 在下面查看我在Codepen上的小项目...

http://codepen.io/ajduff14/pen/DlHge http://codepen.io/ajduff14/pen/DlHge

Basically I'm just testing out my JavaScript knowledge and I want to learn how to change all the buttons to the same class and use JavaScript to input a color that will change the background color of all the buttons with the given class "buttons". 基本上,我只是测试我的JavaScript知识,我想学习如何将所有按钮更改为同一类,并使用JavaScript输入一种颜色,该颜色将更改给定类“按钮”中所有按钮的背景色。 I can only manage to get it to work for 1 class at a time but not several. 我只能设法让它一次只能上一堂课,而不能上几堂课。

It's probably a simple answer but I've done quite a bit of digging for it already and I can't find one! 这可能是一个简单的答案,但是我已经对其进行了很多挖掘,但我找不到! Could someone please help? 有人可以帮忙吗?

Thanks, 谢谢,

Adam 亚当

There is no way to change classes or styles on many elements at the same time, you'll have to iterate and apply it to one element at a time 无法同时更改多个元素上的类或样式,您必须一次迭代并将其应用于一个元素

var buttons = document.querySelectorAll('.buttons');

for (var i=0; i<buttons.length; i++) {

    buttons[i].style.background = 'red';
    buttons[i].className = buttons[i].className + ' newclass';

}

There were some trivial mistakes in your HTML and JS code. 您的HTML和JS代码中有一些琐碎的错误。

1) In many places, instead of class="buttons" for input tag, you specified it as id="buttons". 1)在许多地方,您将输入标签指定为id =“ buttons”而不是class =“ buttons”。 Remember that you should have a unique id for each element. 请记住,每个元素应具有唯一的ID。

2) You were setting the style.backgroundColor property for buttons[3] and not for all objects with class name buttons. 2)您正在为button [3]而不是为所有带有类名按钮的对象设置style.backgroundColor属性。

document.getElementsByClassName('buttons') gets you an array of objects with class name "buttons." document.getElementsByClassName('buttons')为您提供一个类名称为“ buttons”的对象数组。 You need to iterate over every element of this array and set its background color property to appropriate value. 您需要遍历此数组的每个元素,并将其背景颜色属性设置为适当的值。

You could use a for-in loop or a standard for loop to achieve this purpose. 您可以使用for-in循环或标准的for循环来实现此目的。 I have used the for-in loop. 我使用了for-in循环。

for (button in buttons) {
    buttons[button].style.backgroundColor = div2.children[0].value;
}

See this link for a working copy of your project: http://jsfiddle.net/mayank_agarwal/LaQuz/ 请参阅此链接以获取项目的工作副本: http : //jsfiddle.net/mayank_agarwal/LaQuz/

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

相关问题 具有类名的多个复选框更改功能,但在javascript中具有相同的类名 - multiple checkbox change function with class name, but same class name in javascript 使用 JavaScript 选择多个具有相同名称的 DIV - Select multiple DIVs with same name using JavaScript 使用javascript从具有相同名称的所有类中获取数据 - Fetch data from all classes having same name using javascript 如何使用Javascript更改具有相同类名的多个元素? - How do I change multiple elements with same class name using Javascript? 有没有办法使用 JavaScript 更改/添加到 HTML 中的多个 ID/类? - Is there a way to change/add to multiple IDs/Classes within HTML using JavaScript? 如何访问和更改具有相同名称的多个类并循环访问它们以在单击时向它们添加 css 更改? - How to access and change multiple classes with the same name and loop through them to add a css change to them upon click? 声明与JavaScript中存在的数据类型同名的类 - Declaring classes with the same name as DataTypes that exist in JavaScript JavaScript - 同时使用两个同名的类? - JavaScript - Use two classes of the same name simultaneously? 为多个同名类抓取浮点值 - Scraping float values for multiple classes of the same name 如何使用多个具有相同名称的类使用相同的JS函数? - How can I use same JS function using multiple classes of same name?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM