找到' s宽度的JavaScript代码是什么?
(我希望得到宽度.FixedBox
whit javascript no jquery,但我的代码不起作用。)
alert(document.getElementsByClassName('FixedBox').offsetWidth);

<div class="FixedBox">
The HTMLElement.offsetWidth read-only property returns the layout width of an element. Typically, an element's offsetWidth is a measurement which includes the element borders, the element horizontal padding, the element vertical scrollbar (if present, if rendered) and the element CSS width.
</div>
&#13;
答案 0 :(得分:1)
Document.getElementsByClassName()
会返回NodeList,您需要从集合中获取元素以获取 offsetWidth
属性。还要将它们置于窗口加载回调中,以便仅在加载元素后执行。
window.onload = function() {
var ele = document.getElementsByClassName('FixedBox');
alert(ele.length ? ele[0].offsetWidth : 'no elements with the class');
}
&#13;
<div class="FixedBox">
The HTMLElement.offsetWidth read-only property returns the layout width of an element. Typically, an element's offsetWidth is a measurement which includes the element borders, the element horizontal padding, the element vertical scrollbar (if present,
if rendered) and the element CSS width.
</div>
&#13;
答案 1 :(得分:0)
这可能有效
$('.FixedBox').load(function () {
alert(document.getElementsByClassName('FixedBox')[0].getBoundingClientRect().width)
});
alert(document.getElementsByClassName('FixedBox')[0].getBoundingClientRect().width)
<div class="FixedBox">
The HTMLElement.offsetWidth read-only property returns the layout width of an element. Typically, an element's offsetWidth is a measurement which includes the element borders, the element horizontal padding, the element vertical scrollbar (if present,
if rendered) and the element CSS width.
</div>
答案 2 :(得分:0)
试试这个
document.getElementsByClassName('FixedBox')[0].offsetHeight