简体   繁体   English

如何使用 JS 更改网格模板列样式?

[英]How do I change grid-template-columns style with JS?

this is the class I'm trying to change in my CSS file.这是我试图在我的 CSS 文件中更改的 class。

.screen{
margin-top: 0%;
 width: 75%;
 height: 75%;
 background-color: lightgrey;
 display: grid;
 grid-template-columns: repeat(16, 1fr);
 grid-template-rows: repeat(16, 1fr);
}

I assign a size variable and then I assume I can use something like:我分配了一个大小变量,然后我假设我可以使用类似的东西:

document.getElementsByClassName('screen').style = "grid-template-columns: repeat(" + size + ", 1fr)"

but cannot get it to work.但无法让它工作。 :( :(

这是因为document.getElementsByClassName('screen')返回一个数组,因此请尝试使用此数组

document.getElementsByClassName('screen')[0].style

You Simply do the following:您只需执行以下操作:

document.getElementById("yourdivision").style.gridTemplateColumns = "50px 50px 50px";

Or:或者:

document.getElementById("yourdivision").style.gridTemplateColumns = "1fr 1fr 1fr";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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