คุณสมบัติ HTML DOM borderTop ใช้เป็นชวเลขสำหรับการรับหรือตั้งค่าคุณสมบัติเส้นขอบบนสำหรับองค์ประกอบ คุณสมบัติ borderTop ประกอบด้วย border-Top-width, border-Top-style, border-Top-color
ต่อไปนี้เป็นไวยากรณ์สำหรับ −
การตั้งค่าคุณสมบัติ borderTop:
object.style.borderTop = "width style color|initial|inherit"
อธิบายคุณสมบัติข้างต้นดังนี้ −
| พารามิเตอร์ | คำอธิบาย |
|---|---|
| ความกว้าง | สำหรับการตั้งค่าความกว้างขอบด้านบน |
| สไตล์ | สำหรับการตั้งค่ารูปแบบเส้นขอบด้านบน |
| สี | สำหรับการตั้งค่าสีขอบด้านบน |
| เริ่มต้น | สำหรับการตั้งค่าคุณสมบัตินี้เป็นค่าเริ่มต้น |
| สืบทอด | การสืบทอดค่าคุณสมบัติหลัก |
เรามาดูตัวอย่างคุณสมบัติ borderTop กัน −
ตัวอย่าง
<!DOCTYPE html>
<html>
<head>
<style>
#P1 {
border-Top: 4px solid magenta;
font-size: 1.5rem;
}
</style>
<script>
function changeBorderTop(){
document.getElementById("P1").style.borderTop="9px dashed red";
document.getElementById("Sample").innerHTML="The Top border for the paragraph element is now changed ";
}
</script>
</head>
<body>
<p id="P1">This is some sample text inside the paragraph. Here is another line of this sample text</p>
<p>Change the above paragraph Top border properties by clicking the below button</p>
<button onclick="changeBorderTop()">Change Border Top</button>
<p id="Sample"></p>
</body>
</html> ผลลัพธ์

เมื่อคลิกที่ “เปลี่ยนขอบด้านบน” ปุ่ม −

ในตัวอย่างข้างต้น −
ขั้นแรกเราได้สร้างย่อหน้าที่มีรหัส “P1” ที่มีข้อความอยู่ภายในและใช้รูปแบบ css ที่เกี่ยวข้อง
#P1 {
border-Top: 4px solid magenta;
font-size: 1.5rem;
}
<p id="P1">This is some sample text inside the paragraph. Here is another line of this sample text</p> จากนั้นเราก็สร้างปุ่ม “เปลี่ยนขอบด้านบน” ที่จะรันฟังก์ชัน changeBorderTop() เมื่อผู้ใช้คลิก
<button onclick="changeBorderTop()">Change Border Top</button>
ฟังก์ชัน changeBorderTop() รับคุณสมบัติสไตล์ borderTop ขององค์ประกอบย่อหน้าที่มี id “P1” โดยใช้เมธอด getElementById() และเปลี่ยนค่าคุณสมบัติเป็น '9px dashed red' ข้อความที่ระบุการเปลี่ยนแปลงนี้จะแสดงในย่อหน้าที่มีรหัส "ตัวอย่าง" โดยใช้คุณสมบัติ innerHTML -
function changeBorderTop(){
document.getElementById("P1").style.borderTop="9px dashed red";
document.getElementById("Sample").innerHTML="The top border for the paragraph element is now changed";
}