简体   繁体   中英

Using css inside JS code to resize a div

I don't really master css and JS codes as well. And i have to do something like this:

Transforming this code in css:

<div class="siebui-span-lg-6 siebui-span-md-6 siebui-span-sm-12" style="width:60%"></div>

To a JS code.

Any help? Thanks a lot

You can access element style through element.style field, and then set it to some value:

 var divElement = document.querySelectorAll('div.siebui-span-lg-6.siebui-span-md-6.siebui-span-sm-12')[0]; divElement.style.width = "60%"; console.log(divElement.style.width);
 <div class="siebui-span-lg-6 siebui-span-md-6 siebui-span-sm-12"></div>

<script>
var elements= document.getElementsByClassName('siebui-span-lg-6 siebui-span-md-6 siebui-span-sm-12');
for (var i=0; i< elements.length; i++){
      elements[i].style.width="60%";
}
</script>

Or you can add id to div and just:

document.getElementsById('yourId').style.width="60%";

我做了你所有的建议,但它没有返回想要的结果:/ div 不会从他的默认宽度“50%”移动

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