JQuery设置页面加载时div的高度

JQuery设置页面加载时div的高度,jquery,html,height,Jquery,Html,Height,好的,我有一个容器div,它加载一些由php生成的内容。我不知道内容的高度是多少,而且它是绝对定位的,所以容器div在所有内容都存在之前不知道应该有多高。我收集了php变量中需要的高度,并希望使用jquery将容器div设置为该高度,但它还不能工作 我已将此代码放在页面底部附近: <script type="text/javascript"> $(document).ready(function () { $("#DynContainer").css({height:<?php

好的,我有一个容器div,它加载一些由php生成的内容。我不知道内容的高度是多少,而且它是绝对定位的,所以容器div在所有内容都存在之前不知道应该有多高。我收集了php变量中需要的高度,并希望使用jquery将容器div设置为该高度,但它还不能工作

我已将此代码放在页面底部附近:

<script type="text/javascript">
$(document).ready(function () {
$("#DynContainer").css({height:<?php echo $containerHeight; ?>px})
});
</script>
建议不加载jQuery,但它肯定会在页面顶部调用。以下是整个页面的代码:

解决:这是一个无冲突的jQuery问题。
谢谢大家

在值周围加引号,因为它是一个字符串:

<script type="text/javascript">
$(document).ready(function () {
$("#DynContainer").css({height:'<?php echo $containerHeight; ?>px'})
});
</script>

$(文档).ready(函数(){
$(“#DynContainer”).css({height:'px})
});

或者,删除“px”和引号,以便将其解析为整数(“px”是默认单位):


$(文档).ready(函数(){
$(“#DynContainer”).css({height:})
});

由于该值是一个字符串,因此在其周围加上引号:

<script type="text/javascript">
$(document).ready(function () {
$("#DynContainer").css({height:'<?php echo $containerHeight; ?>px'})
});
</script>

$(文档).ready(函数(){
$(“#DynContainer”).css({height:'px})
});

或者,删除“px”和引号,以便将其解析为整数(“px”是默认单位):


$(文档).ready(函数(){
$(“#DynContainer”).css({height:})
});
“px”导致javascript无效。正如其他答案所建议的,您可以在其周围加上引号,也可以完全删除px:

$(document).ready(function () {
    $("#DynContainer").css({height:<?php echo $containerHeight; ?>})
});
$(文档).ready(函数(){
$(“#DynContainer”).css({height:})
});
该“px”导致javascript无效。正如其他答案所建议的,您可以在其周围加上引号,也可以完全删除px:

$(document).ready(function () {
    $("#DynContainer").css({height:<?php echo $containerHeight; ?>})
});
$(文档).ready(函数(){
$(“#DynContainer”).css({height:})
});

我建议在脚本开头将PHP变量传递给JS:

<script type="text/javascript">
var containerHeight = <?php echo $containerHeight; ?>;
// from now, it's pure JS. both better maintainable and readable
$(document).ready(function () {
    $("#DynContainer").css({'height': containerHeight})
});
</script>

var containerHeight=;
//从现在开始,它是纯JS。更好的可维护性和可读性
$(文档).ready(函数(){
$(“#DynContainer”).css({'height':containerHeight})
});

我建议在脚本开头将PHP变量传递给JS:

<script type="text/javascript">
var containerHeight = <?php echo $containerHeight; ?>;
// from now, it's pure JS. both better maintainable and readable
$(document).ready(function () {
    $("#DynContainer").css({'height': containerHeight})
});
</script>

var containerHeight=;
//从现在开始,它是纯JS。更好的可维护性和可读性
$(文档).ready(函数(){
$(“#DynContainer”).css({'height':containerHeight})
});

Hm“DynContainer”拼写正确吗?检查大小写。您需要将440px放入一个字符串(“440px”),因为它是:{height:“440px”}Hm#DynContainer拼写正确吗?检查大小写。您需要将440px放入一个字符串(“440px”),因为它就是这样:{height:“440px”}非常感谢,但我仍然有麻烦。我已经更改了代码,查看javascript生成的页面源代码是:
$(document).ready(函数(){var containerHeight=“440px”;$(“#DynContainer”).css({height:containerHeight})它适合我,看这里:你有对应于DynContainer元素的正确ID吗?我知道这已经非常晚了,但是如果你试图弄乱样式,它可能会工作得更好$(“元素”).attr(“样式”,“高度:”+容器高度);我知道你可能已经解决了这个问题,但我还是有麻烦。我已经更改了代码,查看javascript生成的页面源代码是:
$(document).ready(函数(){var containerHeight=“440px”;$(“#DynContainer”).css({height:containerHeight})它适合我,看这里:你有对应于DynContainer元素的正确ID吗?我知道这已经非常晚了,但是如果你试图弄乱样式,它可能会工作得更好$(“元素”).attr(“样式”,“高度:”+容器高度);我知道你可能已经解决了这个问题
<script type="text/javascript">
var containerHeight = <?php echo $containerHeight; ?>;
// from now, it's pure JS. both better maintainable and readable
$(document).ready(function () {
    $("#DynContainer").css({'height': containerHeight})
});
</script>