在JavaScript中设置DIV高度

在JavaScript中设置DIV高度,javascript,html,Javascript,Html,我创建了一个JSFIDLE来说明我在一些图像中遇到的问题: 双栏布局的HTML如下所示: <div style=""> <div id="divContactTexas" style="width: 50%; float: left; background-color: red;"> Lorem ipsum <img src="

我创建了一个JSFIDLE来说明我在一些图像中遇到的问题:

双栏布局的HTML如下所示:

            <div style="">
                <div id="divContactTexas" style="width: 50%; float: left; background-color: red;">
                    Lorem ipsum 
                    <img src="http://brownfieldsubslab.com/texas.gif" />
                </div>
                <div id="divContactCalifornia" style="width: 50%; float: left; background-color: blue;">
                    Lorem ipsum 
                    <img src="http://brownfieldsubslab.com/california.gif"/>
                </div>
            </div>


            var texasHeight = document.getElementById('divContactTexas').scrollHeight;
            console.log("texasHeight:"+texasHeight);
            var californiaHeight = document.getElementById('divContactCalifornia').scrollHeight;
            console.log("californiaHeight:"+californiaHeight);
            var maxHeight = (texasHeight>californiaHeight) ? texasHeight : californiaHeight;
            console.log("maxHeight:"+maxHeight);    
            document.getElementById('divContactTexas').style.height=maxHeight+'px';
            document.getElementById('divContactCalifornia').style.height=maxHeight+'px';

乱数假文
乱数假文
var texasHeight=document.getElementById('divContactTexas').scrollHeight;
console.log(“texasHeight:+texasHeight”);
var californiaHeight=document.getElementById('divContactCaliforniaHeight').scrollHeight;
log(“californiaHeight:+californiaHeight”);
var maxHeight=(德克萨斯州>加利福尼亚州高度)?德克萨斯州:加利福尼亚州高度;
console.log(“maxHeight:+maxHeight”);
document.getElementById('divContactTexas').style.height=maxHeight+'px';
document.getElementById('divContactCalifornia').style.height=maxHeight+'px';
我在现场网站上有一个模型页面:

因此,我主要在Chrome上遇到了一个问题,当我改变浏览器窗口的大小时,图像越来越窄,当浏览器窗口的宽度很小时,图像开始主要在Chrome上被切断。我已经尝试过在高度上增加更多像素,这确实缓解了问题,但我想知道为什么图像会被切掉,有没有办法防止这种情况

问题开始看起来像:

我已经试着研究一下滚动高度或使用哪个高度似乎并不重要。一定是别的原因

谢谢

将代码包装在函数中,并在调整窗口大小时调用它,最初在加载窗口时调用它

对于图像截断,使用css

#left-side-image{
  width: 100%;
  max-width: 212px;
}
使用CSS


这很有趣。我将CSS直接应用于IMG标记。问题消失了。谢谢你发布这个。
#left-side-image{
  width: 100%;
  max-width: 212px;
}