คุณสมบัติ specified ของ HTML DOM ใช้สำหรับตรวจสอบว่าแอตทริบิวต์ถูกระบุหรือไม่ โดยส่งคืน true หากแอตทริบิวต์ถูกระบุ และ false หากไม่ใช่ จะส่งคืน true ด้วยหากแอตทริบิวต์ถูกสร้างแต่ยังไม่ได้แนบกับองค์ประกอบ
ไวยากรณ์
ไวยากรณ์สำหรับคุณสมบัติ specified:
attribute.specified
ตัวอย่าง
มาดูตัวอย่างสำหรับคุณสมบัติ specified:
<!DOCTYPE html> <html> <body> <p>คลิกปุ่มเพื่อตรวจสอบว่าปุ่มมีแอตทริบิวต์ onclick ที่ระบุหรือไม่</p> <img id="IMG1" src="https://www.tutorialspoint.com/swing/images/swing-mini-logo.jpg" alt="Learn Swing"> <p> ตรวจสอบว่ารูปภาพด้านบนมีแอตทริบิวต์ alt ที่ระบุโดยคลิกปุ่มด้านล่าง </p> <button onclick="checkAlt()">CHECK</button> <p id="Sample"></p> <script> function checkAlt() { var img = document.getElementById("IMG1"); var s=img.getAttributeNode("alt").specified; if(s==true) document.getElementById("Sample").innerHTML = "The image element has alt attribute specified"; else document.getElementById("Sample").innerHTML = "The image element does not have alt attribute specified"; } </script> </body> </html>
ผลลัพธ์
จะได้ผลลัพธ์ดังนี้:

เมื่อคลิกปุ่ม CHECK:
