簡單form標準化執行個體——語義結構_表單特效

來源:互聯網
上載者:User
1、使用fieldset和legend標籤

在form中,我們經常會對form中的資訊進行分組,比如註冊form,我們可能會將註冊資訊分組成基本資料(一般為必填),詳細資料(一般為可選),那我們如何更好的來實現呢?我們可考慮在form中加入下面兩個標籤:
複製代碼 代碼如下:

fieldset:對錶單進行分組,一個表單可以有多個fieldset
legend:說明每組的內容描述

<form id="demoform" class="democss" action=""> <fieldset> <legend>Basic Register</legend> <p>First name: <input type="text" name="fname" value="" /></p> ... </fieldset> <fieldset> <legend>Detailed Register</legend> <p>Interest: <input type="text" name="interest" value="" /></p> ... </fieldset> ... </form>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

fieldset預設是帶邊框的,而legend預設一般顯示在左上方。但在某些場合或許不願意讓fieldset和legend的預設樣式或預設布局影響設計方案中的美觀。

解決方案:在CSS中將fieldset的border設定為0,legend的display設定為none即可。
2、使用label標籤
我們對form中的文字標籤給定一個label標籤,並使用for屬性使其與表單組件相關聯,效果為單擊文字標籤,游標顯示在相對應的表單組件內。
複製代碼 代碼如下:

<form id="demoform" class="democss" action="">
<fieldset>
<legend>Basic Register</legend>
<p>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname" value="" />
</p>
...
</fieldset>
<fieldset>
<legend>Detailed Register</legend>
<p>
<label for="interest">Interest:</label> 
<input type="text" id="interest" name="interest" value="" />
</p>
...
</fieldset>
...
</form>

除了以上方法,我們還可以用label套嵌整個表單組件和文字標籤,如:
<label for="fname">First name:<input type="text" id="fname" name="fname" value="" /></label>
根據規範,文本會自動與鄰接的表單組件關聯,但遺憾的是——現在主流的瀏覽器IE6並不支援這個特性。
3、使用accesskey和tabindex屬性
網站要兼顧更多情況下的使用,比如沒有游標裝置(如滑鼠)的情況下,要讓使用鍵盤操作也可以完成form的填寫,這時候點擊對於它們來說,已經沒有任何意義。我們這個時候選用label的accesskey(快速鍵,IE下為alt+accesskey屬性值,FF下為alt+shift+accesskey屬性值)和tabindex屬性(Tab鍵,tabindex屬性值為順序)添加到表單標籤上,如label,input等。
複製代碼 代碼如下:

<label for="fname" accesskey="f" tabindex="1" >First name:</label> 
<input type="text" id="fname" name="fname" value="" />

4、使用optgroup標籤
optgroup標籤的作用是在挑選清單中定義了一組選項。我們可以選用optgroup標籤給select元素的options分類,並使用label屬性,屬性值會在下拉式清單(select)裡顯示為一個不可選的、縮排標題。注意:optgroup 不支援嵌套。
<select name="China"> <optgroup label="Jiangsu"> <option value="nj">Nanjing</option> <option value="sz">Suzhou</option> </optgroup> <optgroup label="Zhejiang"> <option value="hz">Hangzhou</option> <option value="wz">Wenzhou</option> </optgroup> </select>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]

IE6.0 中存在一個小Bug(FireFox 中不存在):使用鍵盤方向鍵進行選擇時,在 IE 中,當選中項由一個optgroup的選項換成另一optgroup 的選項時,不會觸發onchange。解決辦法是:增加 onkeydown 或 onkeyup 事件協助解決。

5、使用button標籤
複製代碼 代碼如下:

Definition and Usage
Defines a push button. Inside a button element you can put content, like text or images. This is the difference between this element and buttons created with the input element.

定義與用法
定義為一個提交按鈕。在button元素內你可以放置內容,像文本(text)或者圖片(images)。這是這個元素和input元素按鈕的區別。
複製代碼 代碼如下:

<button><img src="images/click.gif" alt="Click Me!" />Click Me!</button> 
button相對於input提供了更多的功能與更豐富的內容。button將按鈕文字單獨出來,並且可以在button內添加圖片,賦予文字和圖片更多選擇的樣式,使生硬的按鈕變得更生動。

並且使用button標籤將比input按鈕來得更有語義化,簡單的從字面意思也可以理解。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.