gpt4 book ai didi

javascript - 如何使用 javascript 修改网格模板区域

转载 作者:太空狗 更新时间:2023-10-29 14:48:36 25 4
gpt4 key购买 nike

我一直在尝试用 javascript 修改 grid-template-areas。我在 DOM 的 css 属性列表 (elem.style) 中找到了一个名为:gridTemplateAreas 的属性。但我似乎无法用javascript改变它。

如何使用 javascript 修改 grid-template-areas-property?

这是我试过的代码:

window.addEventListener("click", function(){
let elem= document.getElementById("grid");
elem.style.gridTemplateAreas =
"z z z"
"a b c"
"d e f"
console.log("The grid-template area should have been redefined now. The blue block should have move to the top row.");
});
#grid{
width: 100px;
height: 100px;
background-color: red;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "x x x"
"y y z"
"y y z";
}
#div1{
background-color: blue;
grid-area: z;
}
<h3>Click to activate the code</h3>
<div id="grid">
<div id="div1"></div>
</div>

PS:我使用的是 Google Chrome 自动取款机

最佳答案

你的 elem.style.gridTemplateAreas =
“兹兹兹”
“a b c”
“d e f”

不是一个有效的声明,或者它没有做你想做的事。您要分配值 "z z z""a b c""d e f"。因此,您需要用这样的引号将其括起来:elem.style.gridTemplateAreas = '"z z z""a b c""d e f"';

window.addEventListener("click", function(){
let elem= document.getElementById("grid");
elem.style.gridTemplateAreas = '"z z z" "a b c" "d e f"';

console.log("The grid-template area should have been redefined now. The blue block should have move to the top row.");
});
#grid{
width: 100px;
height: 100px;
background-color: red;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr;
grid-template-areas: "x x x"
"y y z"
"y y z";
}
#div1{
background-color: blue;
grid-area: z;
}
<h3>Click to activate the code</h3>
<div id="grid">
<div id="div1"></div>
</div>

关于javascript - 如何使用 javascript 修改网格模板区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47394549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com