简体   繁体   English

获取一个div的高度并为另一个div添加值

[英]Get height of a div and add the value for another div

I want to get the height of a div and then to add that value as "top" or "margin-top" to another div. 我想获取一个div的高度,然后将该值作为“ top”或“ margin-top”添加到另一个div。 The first div has dynamical height and at its bottom I want to place another div that contains a link and a fb like button. 第一个div具有动态高度,我想在其底部放置另一个div,该div包含链接和fb之类的按钮。 The html code looks like this: html代码如下所示:

...
<div id="testnew">content</div>
<table>
<tr>
<td id="left">content</td>
<td id="right">
    <div id="test">content</div>
</td>
</tr>
</table>
...

So #testnew is the second div with absolute positioning and right:0;, and #test is the first div with dynamical height. 因此,#testnew是具有绝对位置和right:0;的第二个div,而#test是具有动态高度的第一个div。

I found this script here 我在这里找到了这个脚本

changed it like this: 像这样更改它:

<script type="text/javascript">
var socialNetcss = $("#test").height();
$("#testnew").css({ top: socialNetcss});
</script>

but it didn't work. 但这没用。

I'm a newbie in jQuery/Javascript but I really want to do this thing. 我是jQuery / Javascript的新手,但我真的很想这样做。 Thanks in advance! 提前致谢!

PS: I know I can directly place the second div at the firsts bottom but then I have a problem with the fb like button. PS:我知道我可以直接将第二个div放在第一个底部,但后来我对fb like按钮有问题。

Setting the "top" won't work unless your div #testnew has position style property set to absolute or relative 除非您的div #testnew的位置样式属性设置为绝对或相对,否则设置“顶部”将不起作用

Try "margin-top" instead of "top" in above code 尝试使用“ margin-top”代替上面代码中的“ top”

Example here: jsfiddle 此处的示例: jsfiddle

<div id="first">first<br />first <br />first </div>
<div id="second"></div>​

$(document).ready(function () {
var height= $("#first").height();

$("#second").css({marginTop: height});
});​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM