ในตอนแรก คุณต้องแยกการแยกคลาส div ด้วยความช่วยเหลือของgetElementsByClassName() และวนซ้ำวนรอบ for และใช้เงื่อนไข OR เพื่อแสดงข้อความเฉพาะ
นอกจากนี้ ให้ตั้งค่า yourDiv.style.display='none'
ต่อไปนี้เป็นรหัส JavaScript -
ตัวอย่าง
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div class="block">
header
</div>
<div class="block">
content
</div>
<div class="block">
footer
</div>
<script>
let attribute = document.getElementsByClassName('block');
for (let i = 0; i < attribute.length; i++) {
let impDiv = attribute[i];
let value = impDiv.innerHTML.trim();
if (value == 'header' || value == 'footer') {
impDiv.style.display = 'none';
}
}
</script>
</body>
</html> หากต้องการเรียกใช้โปรแกรมข้างต้น ให้บันทึกชื่อไฟล์ anyName.html(index.html) . คลิกขวาที่ไฟล์และเลือกตัวเลือก open with live server ใน VS code editor
ผลลัพธ์
