源代码:
下载代码
AI 编程工具
点击运行
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> <style> #myDIV { height: 250px; width: 400px; padding: 10px; margin: 15px; border-top: 15px solid black; border-left: 10px solid red; background-color: lightblue; } </style> </head> <body> <p>点击按钮获取 div 元素顶部和左侧边框的宽度:</p> <button onclick="myFunction()">点我</button> <div id="myDIV"> <p id="demo"></p> </div> <script> function myFunction() { var elmnt = document.getElementById("myDIV"); var txt = "顶部边框的宽度: " + elmnt.clientTop + "px<br>"; txt += "左侧边框的宽度: " + elmnt.clientLeft + "px"; document.getElementById("demo").innerHTML = txt; } </script> </body> </html>
运行结果: