简体   繁体   中英

change the width of a group of elements using javascript

I'm writing an HTML CSS JS code and I'm trying to change the style of a group of elements. I already know about document.getElementById(id).style.something = 'somevalue'; but it will require me to write about 25 of those and have 25 different ids. I wonder if there is a way to change the style properties in a class. for example:

CSS code:

div.text
{
width: 100px;
}

HTML code:

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

JavaScript code:

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

"document.getElementsByClass" might not exist but "document.getElementsByClassName" does

It's pretty well supported by all modern browsers here

There is no getElementsByClass , but there is getElementsByClassName :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM