使用javascript将CSS类添加到具有另一个类名称的所有元素 [英] Use javascript to add a CSS class to all elements with another class name
本文介绍了使用javascript将CSS类添加到具有另一个类名称的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用JavaScript将类添加到具有不同类的所有元素。我知道您可能认为这是多余的,但对于我所处的情况,这是必需的。我需要一种方法来查看具有该类名称的所有元素并添加该类,但是我不知道如何获得计数?我正在使用cms进行工作,无法在自己的类中更改样式。
I am trying to use javascript to add a Class to all elements with a different Class. I know you might think this is redundant but for the situation i am in it is needed. i need a way to look though all elements with that class name and add the class but I don't understand how to get a count? I am working with a cms to where I cannot change the styling in the class it self.
$(document).ready(function () {
var ClassBtn = document.getElementsByClassName("buttonSubmit");
ClassBtn.className = ClassBtn.className + " btn";
});
推荐答案
因为看来您正在使用 jQuery :
$(document).ready(function () {
$('.buttonSubmit').addClass('btn');
});
没有jQuery:
window.onload = function() {
var buttons = document.getElementsByClassName("buttonSubmit"),
len = buttons !== null ? buttons.length : 0,
i = 0;
for(i; i < len; i++) {
buttons[i].className += " btn";
}
}
这篇关于使用javascript将CSS类添加到具有另一个类名称的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文