HTML DOM 链接类型属性

htmlweb developmentfront end technology更新于 2025/7/16 5:00:17

链接类型属性设置/返回链接文档的类型。

语法

以下是语法 −

  • 返回 type 属性值
linkObject.type
  • 将 type 设置为有效值
linkObject.type = value

注意  −有效值包括"text/javascript"、"text/css"、"image/gif"等。

示例

让我们看一个链接类型属性的示例 −

<!DOCTYPE html>
<html>
<head>
<title>Link type</title>
<link id="extStyle" rel="stylesheet" href="style.css" type="myStyle">
</head>
<body>
<form>
<fieldset>
<legend>Link-type</legend>
<input type="button" value="Correct Type" onclick="correctType()">
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var extStyle = document.getElementById("extStyle");
   divDisplay.textContent = 'The linked document type: '+extStyle.type+' is not compatible';
   function correctType(){
      extStyle.type = 'text/css';
      divDisplay.textContent = 'Congrats! The linked document type: '+extStyle.type+' is compatible';
   }
</script>
</body>
</html>

在上面的例子中,‘style.css’ 包含 −

form {
   width:70%;
   margin: 0 auto;
   text-align: center;
}
* {
   padding: 2px;
   margin:5px;
}
input[type="button"] {
   border-radius: 10px;
}

输出

这将产生以下输出 −

点击"更正类型"按钮之前 −

点击 "更正类型"按钮之后 −


相关文章


有用资源