萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> XML 程式設計

HTML5 學習筆記(七):新增屬性(下)—— 書寫方向、textarea 增強、表單驗證控制、提交覆寫、選單、樣式作用域、腳本非同步、離線清單、圖標尺寸、列表反序、iframe 沙箱、媒體事件

dirname 屬性

適用 input、textarea,提交時傳送書寫方向(ltr/rtl)。

<form action="addcomment.cgi" method=post>
 <p><label>Comment: <input type=text name="comment" dirname="comment.dir" required></label></p>
 <p><button name="mode" type=submit value="add">Post Comment</button></p>
</form>

提交參數範例:comment=Hello&comment.dir=ltr&mode=add;阿拉伯文環境則為 rtl。

textarea 的 maxlength、wrap 屬性

  • maxlength:同 input,限制最大長度。
  • wrap:列舉值 soft/hard。hard:硬換行(換行符隨表單送出,需配合 cols);soft:軟換行(僅視覺換行,不送出換行符)。

form 的 novalidate 屬性

允許表單跳過驗證直接提交(忽略 required、min、max 等)。

<form action="demo_form.asp" novalidate="novalidate">
 E-mail: <input type="email" name="user_email" />
 <input type="submit" />
</form>

亦可針對特定提交按鈕設 formnovalidate,僅該按鈕跳過驗證:

<form action="editor.cgi" method="post">
 <p><label>Name: <input required name=fn></label></p>
 <p><label>Essay: <textarea required name=essay></textarea></label></p>
 <p><input type=submit name=submit value="Submit essay"></p>
 <p><input type=submit formnovalidate name=save value="Save essay"></p>
 <p><input type=submit formnovalidate name=cancel value="Cancel"></p>
</form>

input/button 的提交覆寫屬性

新增 formaction、formenctype、formmethod、formnovalidate、formtarget;設定後將覆寫所屬 <form> 對應屬性。

<form action="demo_form.asp" method="get">
 First name: <input type="text" name="fname" /><br />
 Last name: <input type="text" name="lname" /><br />
 <input type="submit" value="Submit" />
 <input type="submit" formmethod="post" formaction="demo_post.asp" value="Submit" />
</form>

menu 的 type、label 屬性

允許 menu 轉化為典型使用者介面選單,配合全域 contextmenu 提供右鍵選單。

style 的 scoped 屬性

啟用限定作用域樣式表:規則僅套用至該 style 父元素及其子樹(兄弟樹不受影響)。

<article>
 <h1>Blah Title Blah</h1>
 <p>Blah blah article blah blah.</p>
</article>

<article>
 <style scoped>
 h1 { color: hotpink; }
 article { border: solid 1px hotpink; }
 </style>
 <h1>Blah Title Blah</h1>
 <p>Blah blah article blah blah.</p>
</article>

script 的 async 屬性

外部腳本非同步執行(需為 src 方式)。三種執行模式:

  1. async="async":非同步執行(頁面解析同時執行腳本)
  2. defer="defer"(無 async):頁面解析完成後執行
  3. 無 async 也無 defer:阻塞解析,立即執行
<script type="text/javascript" src="demo_async.js" async="async"></script>

html 的 manifest 屬性

指向應用程式快取清單,配合離線 Web 應用 API。建立 .manifest 檔:

CACHE MANIFEST
#This is a comment

CACHE
index.html
style.css

NETWORK:
search.php
login.php

FALLBACK:
/api offline.html

套用:<html manifest="/offline.manifest">。範例參考:5 步驟建立離線 HTML5 Web App。

link 的 sizes 屬性

搭配 rel="icon" 指定圖標尺寸,允許提供多種尺寸圖標。

<link rel="icon" href="demo_icon.gif" type="image/gif" sizes="16x16" />

ol 的 reversed 屬性

存在時表示列表順序為降序(如倒數前十)。

iframe 的 sandbox、seamless、srcdoc 屬性

允許沙箱內容(如部落格留言)。

<iframe sandbox src="https://usercontent.example.net/getusercontent.cgi?id=12193"></iframe>
<iframe sandbox="allow-same-origin allow-forms allow-scripts" src="https://maps.example.com/embedded.html"></iframe>
<nav><iframe seamless src="nav.include.html"></iframe></nav>

video/audio 的 play 事件屬性

HTML5 將 HTML4 所有 onevent-name 事件處理屬性設為全域屬性,並為媒體元素新增 play 等事件處理屬性。

copyright © 萬盛學電腦網 all rights reserved