ในการกำหนดตำแหน่งที่ถูกต้องขององค์ประกอบ ให้ใช้คุณสมบัติที่ถูกต้องใน JavaScript
ตัวอย่าง
คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อกำหนดตำแหน่งที่ถูกต้องขององค์ประกอบที่อยู่ในตำแหน่งด้วย JavaScript -
<!DOCTYPE html>
<html>
<head>
<style>
#box {
width: 350px;
height: 200px;
background-color: orange;
border: 3px solid red;
position: absolute;
}
</style>
</head>
<body>
<p>Click to set the right position.</p>
<button type = "button" onclick = "display()"> Set Right Position </button>
<div id="box">
<p>This is a div. This is a div. This is a div. This is a div.<p>
<p>This is a div. This is a div. This is a div. This is a div.<p>
<p>This is a div. This is a div. This is a div. This is a div.<p>
</div>
<br>
<br>
<script>
function display() {
document.getElementById("box").style.right = "70px";
}
</script>
</body>
</html>