Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/424.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 无法更改div';jQuery中的s高度_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 无法更改div';jQuery中的s高度

Javascript 无法更改div';jQuery中的s高度,javascript,jquery,html,css,Javascript,Jquery,Html,Css,js divcenter的高度未更改。 此外,第一个警报有效,但第二个警报无效。 控制台是空的 有什么想法吗?试试这个 $(window).load(function() { var a = $("#imgtop").height() - 30; alert (a); // 637 $("#divcenter").css("height", a + "px"); //doesn't work var b = $("#divcenter").height();

js

divcenter
的高度未更改。
此外,第一个警报有效,但第二个警报无效。
控制台是空的

有什么想法吗?

试试这个

$(window).load(function() {
    var a = $("#imgtop").height() - 30;
    alert (a);  // 637
    $("#divcenter").css("height", a + "px"); //doesn't work
    var b = $("#divcenter").height(); 
    alert (b);  // doesn't work
});

你改变了什么?你能稍微解释一下你的答案吗?我在css中添加了{}而不是,使用:它有效吗?你真的从字面上理解了“一点点”,不是吗?看起来你试图通过在javascript中修改补丁来弥补html结构/css的缺点,我建议后退几步,试着找到一个合适的纯css解决方案。我用JSFIDLE试过,效果很好@andrew,我几乎可以肯定没有纯css解决方案可以将绝对定位div的高度调整为相对定位父对象的百分比。@OdaYukimura,我又检查了一遍,不知道为什么它对我不起作用。不管怎样,你帮了我。谢谢。我不知道你的目标是什么,但是如果你想让你所有的div都保持同样的高度,你应该看看flexbox
#divcenter{
    position:absolute;
    z-index:1;
    width:50%;
    left:25%;
    top:25px;
}
$(window).load(function() {
    var a = $("#imgtop").height() - 30;
    alert (a);  // 637
    $("#divcenter").css("height", a + "px"); //doesn't work
    var b = $("#divcenter").height(); 
    alert (b);  // doesn't work
});
$(window).load(function() {
var a = $("#imgtop").height() - 30;
alert (a);  // 637
$("#divcenter").css({"height": a }); //doesn't work
var b = $("#divcenter").height(); 
alert (b);  // doesn't work
});