运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <head> <style> #test { left: 100px; top: 50px; margin: 10px; padding: 10px; width: 300px; position: relative; border: 5px solid black } </style> </head> <body> <div id="test"> <p>Click the button to get the left and top offsets for the test div.</p> <p><button onclick="myFunction()">Try it</button></p> <p id="demo">offsetLeft: <br>offsetTop: </p> </div> <script> function myFunction() { var testDiv = document.getElementById("test"); var demoDiv = document.getElementById("demo"); demoDiv.innerHTML = "offsetLeft: " + testDiv.offsetLeft + "<br>offsetTop: " + testDiv.offsetTop; } </script> </body> </html>