【问题标题】:How to copy an element's height to another element?如何将元素的高度复制到另一个元素?
【发布时间】:2013-04-08 17:18:56
【问题描述】:

我有一个 只有填充的 div,我还有另一个没有任何 css 属性的 div。
我想使用 outerHeight 为两个 div 赋予相同的高度。

这是我的测试,它有效但并不完美: http://jsfiddle.net/A2bNm/6/

$("#container").mouseenter(function () {
        $("#top").animate({ 'padding' : 70 }, "slow");
        $("#bottom").animate({'height':($("#top").outerHeight())}, "slow");

    }).mouseleave(function () {
        $("#top").animate({ 'padding' : 20}, "slow");
        $("#bottom").animate({'height':($("#top").outerHeight())}, "slow");
});

可以帮忙吗?谢谢你,对不起我的英语不好! :)

【问题讨论】:

    标签: jquery jquery-animate outerheight


    【解决方案1】:

    尝试使用当前名称 this 和其他名称 .closest('div).siblings('div') 引用您的 div。

    http://api.jquery.com/siblings/ http://api.jquery.com/closest/

    另外,也许将所需的高度存储为一个自变量,因此它不是基于#top 的当前值(它会发生变化)。

    【讨论】:

    • 我尝试使用this 引用我的div,但不起作用!你能举一个jsfiddle的例子吗?谢谢!
    • 我指的是这个jsfiddle.net/A2bNm/7 作为一种用法,没有实际的完整实现。请注意,我正在选择容器的子项,以便 $(this) 引用每个 div。
    • 非常感谢您!但在 div #top#bottom 我有其他 div,所以如果我使用 $(this) 这指的是所有 div。我尝试将 this 更改为 id 名称,但不起作用。
    • 我想做的只是克隆 div #top 包装器。我尝试使用.clone,但这克隆了整个 div(包含内部内容)。
    • 抱歉,如果没有一些示例,我无法理解您的问题。也许提出另一个问题?
    猜你喜欢
    • 2017-08-16
    • 1970-01-01
    • 2021-10-24
    • 2010-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    相关资源
    最近更新 更多