【问题标题】:How to get the exact actual parents width or height with an absolute positioning for childs?如何通过孩子的绝对定位获得确切的实际父母宽度或高度?
【发布时间】:2013-08-07 04:31:36
【问题描述】:

我正在动态创建元素和子元素,具有不同的绝对位置,子元素有左、上、宽度和高度属性,问题是无论我尝试什么,父元素的宽度和高度始终为 0。检查此 fiddle .

var my_parent = document.getElementById('my_obj');

var chld1 = document.createElement('div');

my_parent.appendChild(chld1);

$(chld1).css({
    position:'absolute',
    left:10, 
    top:50,
    width:60,
    height:20,
    background:'pink'
});

alert($(my_parent).width());

有没有一种简单的方法来获取父级的宽度和高度? (javascript & jQuery,因为 evrything 是动态发生的)

或者我必须创建一个函数来遍历所有子项和子项的子项,检查左侧、顶部的最小值,然后包括子项的宽度和高度……这可能需要我几天时间?还是有神奇的 JavaScript 或 jQuery 插件可以为您提供这些?

【问题讨论】:

    标签: position height width absolute


    【解决方案1】:

    问题在于您孩子的绝对位置。每当子级使用绝对位置时,它都不会填充父级。所以要么不使用绝对定位,要么给父母一个固定的宽度

    【讨论】:

    • 孩子的问题已经出现了,问题确实是这种情况,所以这个答案根本没有帮助,但还是谢谢。
    猜你喜欢
    • 1970-01-01
    • 2014-08-27
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 2019-05-27
    • 2017-12-05
    • 2016-10-22
    相关资源
    最近更新 更多