cha*_*ot7 4 javascript offsetwidth
我有一个JS函数,可以获取页面上div的宽度:
function getWidth() {
var container = document.getElementsByClassName('tag');
var message = "The width of the contents width padding: " + container.width + "px.\n";
alert(message);
}
Run Code Online (Sandbox Code Playgroud)
在加载页面之前,该函数不会运行:
<body onload="getWidth()">
Run Code Online (Sandbox Code Playgroud)
而div在CSS中定义为百分比,最大宽度为900px,这是输出应该是什么.但相反,我得到这个警报:
The width of the contents width padding: undefinedpx.
Run Code Online (Sandbox Code Playgroud)
我环顾四周寻找答案,但看起来这应该是有效的.有什么理由不工作?
epa*_*llo 10
getElementsByClassName返回节点集合.
你表现得像是一个人.
您需要选择索引.
var container = document.getElementsByClassName('tag')[0];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6454 次 |
| 最近记录: |