将类添加到元素 [英] Add class to an element
本文介绍了将类添加到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试编写这些代码以通过CSS制作网格视图:( jsbin)
I try to write these code to make grid view through CSS:( jsbin )
var tables = document.getElementsByClassName('tableData');
var rows = tables[0].getElementsByTagName('tr');
for(var i=1; i<rows.length; i +=2) {
alert(rows[i]);
rows[i].className = "alt";
}
这只适用于tr元素 class =
。但是,如果我想将类添加到tr。
我已经尝试过Core.addClass但它不起作用。
This only works with the tr elements has class=""
. But if I want to add class to tr .
I've tried Core.addClass but it doesn't work .
推荐答案
两年后(2012/06),有一种新的闪亮方法 - element.classList
,方法 add()
, remove()
, toggle()
, contains()
。
Two years later (2012/06), there is a new and shiny approach - element.classList
with methods add()
, remove()
, toggle()
, contains()
.
rows[i].classList.add("alt");
支持
- Chrome 8 +
- Firefox 3.6 +
- Internet Explorer 10 +
- Opera 11.50+
- Safari 5.1 +
- Chrome 8+
- Firefox 3.6+
- Internet Explorer 10+
- Opera 11.50+
- Safari 5.1+
这篇关于将类添加到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文