我正在获取 <p>
的宽度。 <p>
被放置在 <div>
中带有 <input>
的容器在它的旁边。 <div>
并且它的所有内容都在获取宽度之前动态添加到 DOM。
<div id="text-0">
<p>name</p>
<input>
</div>
<p>
如果我查看 $(p)
的宽度控制台中的对象属性是 35( offsetWidth
, clientWidth
),但是 $(p).width()
代码中返回162,也就是旁边输入元素的宽度。所有方法都返回相同的值 $(p).innerWidth()
, $(p).outerWidth()
, p.offsetWidth
, p.clientWidth
, p.getBoundingClientRect()
最佳答案
我发现它是什么 - 我的错误不是我想的 javascript/css 问题。它与在 div
上使用 AngularJs $compile
有关,我没有提到它,因为我认为它与问题无关。解决方案是等待 $compile 然后获取测量值。
关于javascript - 获得真正的p宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53340252/