html新增結構元素解析

本文主要幫助理解HTML5新增結構元素article、section、aside、nav、time微格式。

新增非結構元素header、footer、address、figure。

新增表單元素的屬性form、formaction、formmethod、formenctype、formtarget、required、autofocus、placeholder、list、autocomplete、pattern、indeterminate。

  • section表示頁面上的區域,主要的目的是給文章分段等,section裏必須包含標題。
<section>
    <h1>標題</h1>
    <article>內容</article>
</section>
複製代碼
  • article表示頁面上獨立的區域,和section相比,article更注重自身的獨立性。
<article>
    <section>
        <h1>標題</h1>
        <p>內容</p>
    </section>
</article>
複製代碼
  • aside表示頁面的附加內容,能夠是文章的含義,連接等
<article>
    <section>
        <h1>標題</h1>
         <p>內容</p>
    </section>
</article> 

<aside>
    <h1>評論</h1>
    <section>
        <h2>張</h2>
        <p>好聽</p>
    </section>
</aside>    
複製代碼
  • nav表示頁面的導航,主要包括頁面的主導航,側邊欄導航,頁內導航和分頁導航。
<nav>
    <ul>
        <li><a href="#">111</a></li>
        <li><a href="#">222</a></li>
        <li><a href="#">333</a></li>
    </ul>
</nav>
複製代碼
  • time用datatime屬性表示一個時間戳。T表明分隔符,Z表明格林威治標準時間,**+**表明時區,pubdata表示發佈時間。
<time datatime="2017-01-09T13:02" pubdate>2017-01-09</time>
<time datatime="2017-01-10Z">2017-01-10</time>
<time datatime="2017-01-11+9:00">2017-01-11</time>
複製代碼
  • header標籤用於頁面的頭部、文章的頁眉等。頁面中能夠出現多個header標籤。
<header>
  <h1>這是標題</h1>
</header>
<article>
  <header>
    <h2>這是文章標題</h2>
  </header>
</article>
複製代碼
  • footer標籤用於頁面的註腳、文章的頁腳等。頁面中能夠出現多個footer標籤。
<article>
  <footer>
    <h2>這是文章頁腳</h2>
  </footer>
</article>
<footer>
  <h1>這是註腳</h1>
</footer>
複製代碼
  • address標籤用於表示文檔的做者,聯繫地址,用在body裏表示文檔的做者,用在article裏表示文章的做者,一般和footer搭配使用。
<address>
  <ul>
    <li>姓名</li>
    <li>地址</li>
  </ul>
</address>
複製代碼
  • figure標籤表示獨立的內容,figcaption表示figure的標題。figcaption標籤贏始終置於figure的第一位或最後一位
<figure>
  <figcaption>這是內容</figcaption>
  <p>這裏是文字</p>
</figure>
複製代碼
  • 新增屬性form表示輸入域所屬的一個或多個表單。
<form id="userInfo">
地址:<input  type="text" />
<input type="submit" />
</form>
姓名: <input from="userInfo" type="text" />
複製代碼
  • formaction能夠重寫表單action屬性
<form id="userInfo" action="index.jsp">
姓名: <input name="name" type="text" />
地址:<input name="address" type="text" />
<input type="submit" formaction="userInfo.jsp" />
</form>
複製代碼
  • formenctype能夠重寫表單enctype屬性
<form id="userInfo" enctype="text/plain">
姓名: <input name="name" type="text" />
地址:<input name="address" type="text" />
<input type="submit" formenctype="multipart/form-data" />
</form>
複製代碼
  • formmethod能夠重寫表單method屬性。name屬性爲key,value屬性爲value
<form id="userInfo" method="post">
姓名: <input name="name" type="text" />
地址:<input name="address" type="text" />
<input type="submit" formmethod="get" />
</form>
複製代碼
  • formtarget能夠重寫表單target屬性。
<form id="userInfo" target="_self">
姓名: <input name="name" type="text" />
地址:<input name="address" type="text" />
<input type="submit" formtarget="_block" />
</form>
複製代碼

以上屬性只適用於type="submit"html

  • required屬性規定是必填字段。不符合規則的會彈出提示。
<form id="userInfo">
姓名: <input name="name" type="text" required />
地址:<input name="address" type="text" />
<input type="submit" />
</form>
複製代碼
  • autofocus規定當頁面加載完成時自動得到焦點。
<form id="userInfo">
姓名: <input name="name" type="text" required autofocus/>
地址:<input name="address" type="text" />
<input type="submit" />
</form>
複製代碼
  • placeholder規定文本框未獲取焦點時的文本內容。
<form id="userInfo">
姓名: <input name="name" type="text" placeholder="請輸入用戶名"/>
地址:<input name="address" type="text" />
<input type="submit" />
</form>
複製代碼
  • list屬性能夠使單行文本框得到焦點時顯示一個菜單可供選擇,容許自行輸入內容。
<form id="userInfo">
姓名: <input name="name" type="text" />
地址:<input name="address" type="text" list="citys" />
            <datalist id="citys">
                <option value="北京">北京</option>
                <option value="上海">上海</option>
            </datalist>
<input type="submit" />
</form>
複製代碼
  • autocomplete屬性可以使瀏覽器根據用戶已輸入的內容顯示用戶過去輸入過的內容。
<form id="userInfo">
姓名: <input name="name" type="text" autocomplete="on" />
地址:<input name="address" type="text" />
<input type="submit" />
</form>
複製代碼
  • pattern屬性規定本字段的驗證模式。不符合規則的會彈出提示。
<form id="userInfo">
姓名: <input name="name" pattern="[a-z]{1,5}" type="text" />
地址:<input name="address" type="text" />
<input type="submit" />
</form>
複製代碼
  • indeterminate屬性是checked複選框的第三種狀態:模糊狀態。單獨用在html裏不會起做用。
<body>
  <form id="userInfo">
    <input type="checkbox" id="check" name="vehicle" value="Bike" /> I have a bike
  </form>
</body>
<script>
  var check = document.getElementById("check");
  check.indeterminate = true;
</script>
複製代碼
相關文章
相關標籤/搜索