使用javascript更改一组元素的宽度

时间:2013-01-03 21:18:21

标签: javascript html css

我正在编写HTML CSS JS代码,我正在尝试更改一组元素的样式。我已经了解了document.getElementById(id).style.something ='somevalue';但它需要我写出其中的25个并且有25个不同的ID。我想知道是否有办法改变类中的样式属性。例如:

CSS代码:

div.text
{
width: 100px;
}

HTML code:

<div class="text">Some Text</div> <!-- and bunch of other divs like it !>

JavaScript代码:

function changeWidth() {
document.getElementsByClass('text').width = '200px'; 
// I know getElementByClass doesn't exist I wanna know if there is something that would do that.
}

2 个答案:

答案 0 :(得分:3)

“document.getElementsByClass”可能不存在,但"document.getElementsByClassName" does

所有现代浏览器here

都支持它

答案 1 :(得分:1)

没有getElementsByClass,但有getElementsByClassName

https://developer.mozilla.org/en-US/docs/DOM/document.getElementsByClassName