gpt4 book ai didi

javascript - 用 JS 改变 .css?使用变量设置动态 .css 属性

转载 作者:行者123 更新时间:2023-11-30 08:48:57 25 4
gpt4 key购买 nike

我有类似下面的内容:

<div id="left">
left
</div>
<div id="right">
right
</div>

使用 .css 属性:

#left {
width: 60%;
float: left
min-height: ###
max-height: 1000px;
}
#right {
width: 40%;
float: right;
min-height: ###
max-height: 1000px;
}

注意 ###对于<div> CSS min-height特性。我想要类似下面的东西(一些伪 JS):

var leftheight = document.getElementById(left);
var rightheight = document.getElementById(right);
if (leftheight.currentHeight > rightheight.currentHeight) {
rightheight.style.min-height = leftheight.currentHeight;
} else if (rightheight.currentHeight > leftheight.currentHeight) {
leftheight.style.min-height = rightheight.currentHeight;
}

基本上我想要:

if (current height of left > current height of right) {
min-height of right = current height of left
} else if (current height of right > current height of left) {
min-height of left = current height of right
}
//ie. both left and right have the same min-heights, whichever is larger

我的 Javascript 是错误的,这是我刚刚学习的东西。有什么方法可以用来获得我想要的结果吗?

最佳答案

你可以这样做:

if (leftheight.currentHeight > rightheight.currentHeight) {
rightheight.style.minHeight = leftheight.currentHeight;
} else if (rightheight.currentHeight > leftheight.currentHeight) {
leftheight.style.minHeight = rightheight.currentHeight;
}

实际上是 minHeight 而不是 min-height

关于javascript - 用 JS 改变 .css?使用变量设置动态 .css 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19561979/

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