JQuery在PX中获取Element宽度

时间:2012-09-22 22:17:28

标签: jquery html css

我有一个固定的DIV,它沿着页面底部运行,并且有几个DIV,它使用css width%设置。

例如:HTML

    <div id="footerHide" class="footerDivRightContainers">
      <img src="../../images/footer-divider.png" id="footerDividerHide" class="footerDivider">
      <h3 id="footerHide">Hide</h3>
    </div>

CSS:

div#footerHide {
    position: relative;
    cursor: pointer;
    width: 6%;
    min-width: 50px;
}

我使用百分比,因此它会重新调整页面大小。

我需要以像素为单位获得宽度,然后运行:

alert($('div#footerHide').width());

我得到6的结果 - 与宽度有关:6%。

有没有办法在Pixels中获取此值?

THX

2 个答案:

答案 0 :(得分:1)

jQuery width()返回元素的计算宽度值。您的错误无法通过jsfiddle与他们支持的任何较新版本的jQuery或您提供的代码重现。

这可能是一个issue with retrieving a width value for a hidden element with generated content - 因为你说的代码更多,我只能假设没有提供足够详细的代码片段。

答案 1 :(得分:0)

尝试将此用于webkit和mozilla

alert(window.getComputedStyle(document.getElementById("footerHide")).width);

这适用于IE

alert(document.getElementById("footerHide").currentStyle.width);