HTML DOM specified 属性

htmlweb developmentfront end technology更新于 2024/5/28 2:20:17

HTML DOM specified 属性用于检查某个属性是否已指定。如果属性已指定,则返回 true,否则返回 false。如果给定属性已创建但尚未附加到元素,则也返回 true。

语法

以下是指定属性的语法 −

attribute.specified

示例

让我们看一下 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.cainiaomax.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>

输出

这将产生以下输出 −

点击"检查"按钮 −


相关文章


有用资源