开发者

Reading an element's height property as a string with alert()

This 开发者_开发问答is pretty rudimentary, but I want to know how I can access a property like height as a string. The mouseover alert here simply returns "undefined" (and saying height.value in the alert doesn't help either):

<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>


This is because most HTML elements do not have a height property (and those that have may not necesarely indicate the height in pixel). So the height attribute you added to the div-tag is ignored by the browser. Use css styles to define the heights of div's and other elements:

<div id="wut" align="center" style="height: 10px" onmouseover="alertheight()">

What I guess you are looking for however is likely clientHeight and similar properties: https://developer.mozilla.org/en/DOM/element.clientHeight

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜