HTML 标题

htmlweb developmentfront end technology更新于 2024/2/27 18:20:17

HTML 标题指的是从 <h1> 到 <h6> 的 6 个级别,其中 h1 是最重要的标题级别,h6 是最低重要性的标题级别。H1、H2、H3、H4、H5、H6 是六个标题。

注意 − 标题被搜索引擎(例如 Google)用于索引网页内容。

让我们看一个 HTML 标题 的示例 −

示例

<!DOCTYPE html>
<html>
<head>
<title>HTML Heading</title>
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"] {
      border-radius: 10px;
   }
</style>
</head>
<body>
<form>
<fieldset>
<legend>HTML Heading</legend>
<input type="text" id="textSelect" placeholder="Type Heading Here...">
<div id="radioBut">
<label>H1</label><input class="radio" type="radio" name="heading" value="h1" checked>
<label>H2</label><input class="radio" type="radio" name="heading" value="h2">
<label>H3</label><input class="radio" type="radio" name="heading" value="h3">
<label>H4</label><input class="radio" type="radio" name="heading" value="h4">
<label>H5</label><input class="radio" type="radio" name="heading" value="h5">
<label>H6</label><input class="radio" type="radio" name="heading" value="h6">
</div>
<div>Heading Preview: <span id="headingPreview">Output will show up here</span></div>
<input type="button" onclick="changeHeading()" value="Preview">
</fieldset>
</form>
<script>
   var textSelect = document.getElementById("textSelect");
   var headingPreview = document.getElementById("headingPreview");
   function changeHeading() {
      if(textSelect.value === '')
         headingPreview.innerHTML = 'Write a Heading';
      else{
         for(var i=0; i<6; i++){
            var radInp = document.getElementsByClassName('radio')[i];
            if(radInp.checked === true && textSelect.value !== '')
               headingPreview.innerHTML ='<'+radInp.value+'>'+textSelect.value+'</'+radInp.value+'>';
         }
      }
   }
</script>
</body>
</html>

输出

这将产生以下输出 −

1) 点击 ‘Preview’按钮之前,文本字段为空 −

2) 点击‘Preview’按钮之后,文本字段为空 −

3) 点击‘Preview’按钮之后,文本字段不为空, ‘h6’ 单选按钮已选中 −

4) 点击‘预览’按钮后,文本字段不为空,且‘h1’ 单选按钮已选中 −


相关文章


有用资源