无法设置属性'高度'的未定义

Cannot set property 'height' of undefined

本文关键字:未定义 高度 设置 属性      更新时间:2023-09-26

我试图通过从另一个div获取height来更改divheight,但我得到了错误Cannot set property 'height' of undefined

代码:

$("#loading_image_container").style.height = $('#content_frame').height();

我确信content_frame的高度是500px。

尝试

$("#loading_image_container").height($('#content_frame').height());

因为您引用的是jQuery对象,而不是DOM元素。使用内置的jQuery函数如下:

$("#loading_image_container").height($('#content_frame').height());

或者,首先获取DOM元素:

$("#loading_image_container")[0].style.height = $('#content_frame').height();

您正试图设置jQuery对象的style,请使用CSS

$("#loading_image_container").css("height", $('#content_frame').height());

或者:

$("#loading_image_container")[0].style.height = $('#content_frame').height();

试试这个:

$(".div1").css({'height':($(".div2").height()+'px')});

也许您错过了正确表达式中的.style:尝试

  $("#loading_image_container").style.height = $('#content_frame').style.height;