HTML DOM 锚 type 属性


与锚标记关联的 HTML DOM type 属性用于设置或获取链接的 type 属性的值。此属性是在 HTML 5 中引入的。此属性也仅用于推荐目的,并且不强制包含。它包含一个 MIME(多用途互联网邮件扩展)值类型。

语法

以下是语法 −

返回 type 属性 −

anchorObject.type

设置 type 属性 −

anchorObject.type = MIME-type

示例

让我们看一个用于锚文本属性的示例 −

 实时演示

<!DOCTYPE html>
<html>
<body>
<p><a id="Anchor" type="text/html" href="https://www.examplesite.com">example site</a></p>
<p><a id="Anchor2" href="https://www.example.com">example</a></p>
<p>Click the buttons to set and get the type attribute.</p>
<button onclick="getType1()">GetType</button>
<button onclick="setType2()">SetType</button>
<p id="Type1"></p>
<script>
   function getType1() {
      var x = document.getElementById("Anchor").type;
      document.getElementById("Type1").innerHTML = x;
   }
   function setType2(){
      document.getElementById("Type1").innerHTML="Type has been set";
      document.getElementById("Anchor2").type="text/html";
   }
</script>
</body>
</html>

输出

将生成以下输出 −

点击 GetType 按钮 −

点击 SetType 按钮 −

在上面的示例中 −

我们分别使用了 ID Anchor 和 Anchor2 引用了两个链接。Anchor1 具有与之关联的 MIME 类型 text/html,而 Anchor2 没有与之关联的任何 MIME 类型。

<p><a id="Anchor" type="text/html" href="https://www.examplesite.com">example site</a></p>
<p><a id="Anchor2" href="https://www.example.com">example</a></p>

然后,我们有两个按钮 GetType 和 SetType,分别执行函数 getType1() 和 getType2() 。

<button onclick="getType1()">GetType</button>
<button onclick="setType2()">SetType</button>

getType1() 函数返回与具有 ID “Anchor” 的锚标记关联的 type。setType2() 函数将具有 ID “Anchor2” 的锚标记的类型设置为 text/html。

function getType1() {
   var x = document.getElementById("Anchor").type;
   document.getElementById("Type1").innerHTML = x;
}
function setType2(){
   document.getElementById("Type1").innerHTML="Type has been set";
   document.getElementById("Anchor2").type="text/html";
}

更新于:20-Feb-2021

106 次浏览

开启你的 职业生涯

完成课程,获得认证

开始
广告
© . All rights reserved.