HTML DOM 锚点 type 属性
htmlweb developmentfront end technology更新于 2024/6/11 16:12:17
与锚点标签关联的 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"的锚标签的类型。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";
}
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

