使用alert()将元素的height属性读取为字符串

Ama*_*nus 2 html javascript

这是非常简陋的,但我想知道如何以高度作为字符串访问属性.这里的mouseover警报只返回"undefined"(并且警告中的height.value也没有帮助):

<html>
 <body>
  <div id="wut" align="center" height="10" onmouseover="alertheight()">
    hi.
  </div>
  <script type="text/javascript">
    function alertheight() {
        alert(document.getElementById("wut").height);
    }
  </script>
 </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Jan*_*net 5

这是因为大多数HTML元素没有高度属性(并且那些可能没有必要指示高度的像素).因此,浏览器会忽略您添加到div-tag的height属性.使用css样式定义div's和其他元素的高度:

<div id="wut" align="center" style="height: 10px" onmouseover="alertheight()">
Run Code Online (Sandbox Code Playgroud)

我猜您正在寻找的可能clientHeight和类似的属性:https://developer.mozilla.org/en/DOM/element.clientHeight