คุณสมบัติที่ระบุ HTML DOM ใช้สำหรับตรวจสอบว่ามีการระบุแอตทริบิวต์หรือไม่ ทำได้โดยคืนค่า จริง หากแอตทริบิวต์ถูกระบุและเป็นเท็จ นอกจากนี้ยังจะส่งกลับจริงหากมีการสร้างแอตทริบิวต์ที่กำหนดแต่ยังไม่ได้แนบกับองค์ประกอบ
ไวยากรณ์
ต่อไปนี้เป็นไวยากรณ์สำหรับคุณสมบัติที่ระบุ -
attribute.specified
ตัวอย่าง
ให้เราดูตัวอย่างของคุณสมบัติที่ระบุ -
<!DOCTYPE html>
<html>
<body>
<p>Click the button find out if the button has an onclick attribute specified.</p>
<img id="IMG1" src="https://www.tutorialspoint.com/swing/images/swing-mini-logo.jpg"
alt="Learn Swing">
<p> Check if the above image has alt attribute specified by clicking the below button </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> ผลลัพธ์
สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -

เมื่อคลิกปุ่มตรวจสอบ -
