CSS 使用 jquery 从 div 'height' 设置 div 'top'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9737466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:13:09  来源:igfitidea点击:

CSS set div 'top' with jquery from div 'height'

jquerycss

提问by Denny Mueller

I have a template with lots of absolute positioning, now my main text div has a dynamic height and I need a dynamic footer which follows after this.

我有一个有很多绝对定位的模板,现在我的主文本 div 有一个动态高度,我需要一个动态页脚,在此之后。

I tried to set the top state of the footer div with the height value from the text div.

我试图用文本 div 的高度值设置页脚 div 的顶部状态。

 <script type="text/javascript">
    $("#footer").top($("#text").height());
    </script>

Unfortunately, Chrome Dev tools throws the error:

不幸的是,Chrome 开发工具抛出错误:

Uncaught TypeError: Object [object Object] has no method 'top' (anonymous function)

未捕获的类型错误:对象 [object Object] 没有方法“top”(匿名函数)

So any idea or help for my problem?

那么对我的问题有什么想法或帮助吗?

Edit:

编辑:

So i tried

所以我试过了

 $('#footer').css('top', $('#text').outerHeight() + 'px');

and it works well, is it possible to add some px to this top value?

并且效果很好,是否可以在此最高值上添加一些 px?

回答by dougajmcdonald

You will need to set the css property 'top' on the #footer div, not call .top() on the div itself.

您需要在#footer div 上设置css 属性“top”,而不是在div 本身上调用.top()。

$("#footer").css('top', $("#text").height() + "px");

or along those lines

或沿着这些路线

回答by Blazemonger

Replace .topwith .offset({top: somenumber})

替换.top.offset({top: somenumber})

http://api.jquery.com/offset/

http://api.jquery.com/offset/

回答by Th0rndike

try this:

尝试这个:

    $('#footer').style.css("top",$('#text').style.css("height"));

回答by edsk

I would suggest:

我会建议:

<script type="text/javascript">
    jQuery(document).ready(function($){
        $('#footer').css('top', $('#text').outerHeight() + 'px');
    });
</script>

回答by jAndy

The debugger is correct, jQuery doesn't abstract a method top. You would rather use .css()like

调试器是正确的,jQuery 没有抽象方法top。你宁愿使用.css()

$('#footer').css('top', $('#text').height());