【问题标题】:is it possible to get the height of a cloned jQuery object?是否可以获得克隆的 jQuery 对象的高度?
【发布时间】:2011-11-26 08:16:13
【问题描述】:

示例 jQuery:

$('document').ready(function(){
    var $theP = $('p');
    var $theDiv = $('div');
    $theP.html($theP.html() + "<br>div height: " + $theDiv.outerHeight());
    var $theClone = $theDiv.clone();
    $theP.html($theP.html() + "<br>clonded div height: " + $theClone.outerHeight());
})

直播链接:http://jsbin.com/odujiv/4

在上面运行时,在尝试获取其高度时会得到“0”的结果。我认为这是因为克隆的对象还没有被放入 DOM 中。获得高度以首先将克隆的元素添加回 DOM 的唯一方法是什么?如果是这样,我认为这是可行的,但在我将其放回 DOM 之前处理它会很棒。

【问题讨论】:

  • 这里有一个类似的“hacky”解决方案:stackoverflow.com/questions/2345784/…
  • 是的,我想解决方案是我必须在获得它的高度之前将它添加到 DOM。那可能会奏效。我可以将它添加到 DOM,将其放置在屏幕外,获取高度,进行计算,然后将其放置在我想要的位置。
  • 我之前遇到过这个问题——添加到屏幕外的 DOM 就可以了。

标签: jquery height clone


【解决方案1】:

不,很遗憾。原始 HTML 元素在未附加到文档时没有大小,并且 jQuery 尚未实现解决方法。我认为这是因为 HTML 元素的大小相互依赖!

【讨论】:

    【解决方案2】:

    试试这个:

    $theClone.height();
    
    or
    
    $theClone.css('height');
    

    【讨论】:

    • 我特别需要outerHeight,但问题是一样的。上面也返回 0
    猜你喜欢
    • 2016-07-22
    • 1970-01-01
    • 2010-10-29
    • 1970-01-01
    • 2010-09-09
    • 2010-10-22
    • 2019-08-30
    相关资源
    最近更新 更多