คุณสมบัติ HTML DOM fontWeight ใช้สำหรับการตั้งค่าหรือส่งคืนความหนาของอักขระข้อความขององค์ประกอบ
ต่อไปนี้เป็นไวยากรณ์สำหรับ −
การตั้งค่าคุณสมบัติ fontWeight -
object.style.fontWeight = "normal|lighter|bold|bolder|value|initial|inherit"
อธิบายคุณสมบัติข้างต้นดังนี้
| ค่า | คำอธิบาย |
|---|---|
| ปกติ | นี่คือค่าเริ่มต้นและจะไม่เปลี่ยนแปลงแบบอักษรใดๆ |
| เบากว่า | ไฟแช็กฟอนต์ |
| ตัวหนา | กำหนดแบบอักษรให้เป็นตัวหนาซึ่งหนากว่าสีอ่อน |
| หนาขึ้น | ตั้งค่าฟอนต์ให้เข้มขึ้นซึ่งหนากว่าตัวหนา |
| 100 200 300 400 500 600 700 800 900 | ค่าช่วงของค่า Givesa ที่แสดงแสงเป็นอักขระตัวหนา ปกติ=400,700=ตัวหนา |
| ค่าเริ่มต้น | การตั้งค่าคุณสมบัตินี้เป็นค่าเริ่มต้น |
| สืบทอด | การสืบทอดค่าคุณสมบัติหลัก |
เรามาดูตัวอย่างคุณสมบัติ fontWeight กัน −
ตัวอย่าง
<!DOCTYPE html>
<html>
<head>
<style>
#demo2,#demo1 {
font-family: 'times new roman';
font-size: 25px;
}
</style>
<script>
function changeFontWeight() {
document.getElementById("demo1").style.fontWeight="bold";
document.getElementById("demo2").style.fontWeight="bold";
document.getElementById("Sample").innerHTML="The font weight has been changed for the above paragraphs";
}
</script>
</head>
<body>
<div id="demo1" >This is demo text</div>
<div id="demo2">This is demo text</div>
<p>Change the font weight for the text inside above divs by clicking the below button</p>
<button onclick="changeFontWeight()">Change font Weight </button>
<p id="Sample"></p>
</body>
</html> ผลลัพธ์

เมื่อคลิกปุ่ม “เปลี่ยนน้ำหนักแบบอักษร ปุ่ม −
