源代码:
下载代码
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> <style> #myDIV { margin-top: 10px; height: 250px; width: 250px; overflow: auto; } #content { height: 800px; width: 2000px; padding: 10px; background-color: coral; } </style> </head> <body> <p>点击按钮获取 id="content" 元素的高度(垂直)和宽度(水平)。</p> <button onclick="myFunction()">点我</button> <div id="myDIV"> <div id="content">正文部分..</div> </div> <p id="demo"></p> <script> function myFunction() { var elmnt = document.getElementById("content"); var y = elmnt.scrollHeight; var x = elmnt.scrollWidth; document.getElementById ("demo").innerHTML = "高度为: " + y + "px<br>宽度为: " + x + "px"; } </script> </body> </html>
运行结果: