获取变量div height和plant var到另一个css选择器

时间:2011-11-17 14:20:39

标签: javascript jquery height getelementbyid var

天才,这可能吗?请帮忙,因为我的jQuery javascripting很糟糕。

我需要获得div的高度,这是动态创建的(默认情况下也是自动高度) - 动态推文内容也是由javascript生成的,我希望不会导致问题。

看我的动态div ......

<div id="tweet-area"></div>

然后我需要在下面的脚本中植入 div#tweet-area 高度...

$("#sidebar-wrapper").css({ bottom: " + variable here + " });

所以这是我的尝试,但不是很有效......

var tweetheight = document.getElementById('tweet-area').offsetHeight;

$("#sidebar-wrapper").css({ bottom: " + tweetheight + " });

任何建议都会非常有用,谢谢。

约什


更新

这是有效的脚本 - 在推文脚本之后加载

var tweetheight = $("#tweet-area").height();
$("#sidebar-wrapper").css({ bottom: tweetheight});

谢谢大家!

2 个答案:

答案 0 :(得分:2)

也许$("#sidebar-wrapper").css("bottom", tweetheight);可以做到吗?

var tweetheight = $("#tweet-area").height();

在获得元素的高度方面对我有用^

答案 1 :(得分:1)

为什么要在引号中加上变量名?这使它成为一个字符串。试试这个:

var tweetheight = document.getElementById('tweet-area').offsetHeight;
$("#sidebar-wrapper").css({ bottom: tweetheight});
但是,除非必要,否则我建议不要使用jQuery。没有像这样的jQuery可以轻松更改CSS属性:

document.getElementById('sidebar-wrapper').style.bottom = tweetheight + 'px';
相关问题