• 首页
  • vue
  • TypeScript
  • JavaScript
  • scss
  • css3
  • html5
  • php
  • MySQL
  • redis
  • jQuery
  • <fieldset>

    fieldset 元素通常用来对表单中的控制元素进行分组(也包括 label 元素)

    浏览器支持

    所有主流浏览器都支持<fieldset>标签。

    示例

    一个拥有<fieldset>,<legend ><label>的表单

    <form action="test.php" method="post">
      <fieldset>
        <legend>title</legend>
        <input type="radio" id="radio"> <label for="radio">click me</label>
      </fieldset>
    </form>
    
    title

    标签定义及使用说明

    • <fieldset>标签可以将表单内的相关元素分组。<fieldset>标签将表单内容的一部分打包,生成一组相关表单的字段。
    • 当一组表单元素放到<fieldset>标签内时,浏览器会以特殊方式来显示它们,它们可能有特殊的边界、3D 效果,或者甚至可创建一个子表单来处理这些元素。
    • <fieldset>标签会在相关表单元素周围绘制边框。
    • <legend>标签为<fieldset>元素定义标题。
    • <fieldset>标签没有必需的或唯一的属性。
    Content categories流式内容元素,sectioning root,listed,form-associatedelement,段落内容元素.
    Permitted contentAn optional<legend>element, followed by flow content.
    Tag omission不允许,开始标签和结束标签都不能省略。
    允许的父元素可接受的任何元素flow content.
    DOM接口HTMLFieldSetElement

    注意:与几乎任何其他元素不同,WHATWG HTML Rendering规范建议min-width:min-content作为<fieldset>默认样式的一部分,并且许多浏览器都实现了这种样式(或者与其类似的东西).

    属性

    属性描述
    disabled HTML5新增disabled规定该组中的相关表单元素应该被禁用。
    form HTML5新增form_id规定 fieldset 所属的一个或多个表单。
    name HTML5新增text规定 fieldset 的名称。

    HTML 4.01 与 HTML 5 之间的差异

    HTML5 中新增了一些<fieldset>的新属性:disabled、form、name,HTML 4.01 中不支持这些属性。

    全局属性

    <fieldset>标签支持HTML的全局属性。

    事件属性

    <fieldset>标签支持HTML的事件属性。

    实例

    对表单中的相关元素进行分组:

    <form>
    <fieldset>
    <legend>personalia:</legend>
    name:<input type="text"><br>
    email:<input type="text"><br>
    date of birth:<input type="text">
    </fieldset>
    </form>

    利用包含radioboxesandtextboxes的 fieldset 模拟一个可编辑的<select>元素。下面的例子只使用了 HTML 和 CSS.

    请注意,屏幕朗读和辅助设备不能正确的解析下面的表单,如果不使用正确的元素这个例子是不适合在生产环境中使用.

    <!doctype html>
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>editable [pseudo]select</title>
    <style type="text/css">
    
    /* generic form fields */
    
    fieldset.elist, input[type="text"], textarea, select, option, fieldset.elist ul, fieldset.elist > legend, fieldset.elist input[type="text"], fieldset.elist > legend:after {
      -webkit-box-sizing: border-box;
      -moz-box-sizing: border-box;
      box-sizing: border-box;
    }
    
    input[type="text"] {
      padding: 0 20px;
    }
    
    textarea {
      width: 500px;
      height: 200px;
      padding: 20px;
    }
    
    textarea, input[type="text"], fieldset.elist ul, select, fieldset.elist > legend {
      border: 2px #cccccc solid;
      border-radius: 10px;
    }
    
    input[type="text"], fieldset.elist, select, fieldset.elist > legend {
      height: 32px;
      font-family: tahoma;
      font-size: 14px;
    }
    
    input[type="text"]:hover, textarea:hover, select:hover, fieldset.elist:hover > legend {
      background-color: #ddddff;
    }
    
    select {
      padding: 4px 20px;
    }
    
    option {
      height: 30px;
      padding: 5px 4px;
    }
    
    option:not(:checked), textarea:focus {
      background-color: #ffcccc;
    }
    
    fieldset.elist > legend:after, fieldset.elist label {
      height: 28px;
    }
    
    input[type="text"], fieldset.elist {
      width: 316px;
    }
    
    input[type="text"]:focus {
      background: #ffcccc url("data:image/gif;base64,r0lgodlheaaqanu5apnoxuvr6+uxpdvb2+rq6ri4uo7qxunp6dpt06shv+/rx8vly+nezlo0sbe3t9ksas+qaacev8rkyp2dnf39/qaaak6ursifzhfxcc/qzu3mxyymjexcjnv1dc6mao7u7o+pj2txnoaghtfdpkcdvu3lxm+tcakev9bw1qofvwnjy8kristexnra2nbbobgxsby8vo/mu7kyso9zauzs7msgaiikhf///8zmzp///waaaaaaaaaaaaaaaaaaaaaaach5baeaadkalaaaaaaqabaaaaaxwjxwscwoyzwkmpkkzmoaqdqajdpqaqw2m53nrjlboaarfczomce0c99o8dgnmvm8tm3bbydr9x11dwkzdg5yc2oqjircenx/mxoeetm2q3pxatmlf4mylo17oasdlispmyaioiy0bzmcitmtkbasjgssftmqgxitmjyuotqbbaqhxge0wzcfmtdrmi/qra022nnang1cqqa7") no-repeat 2px center !important;
    }
    
    input[type="text"]:focus, textarea:focus, select:focus, fieldset.elist > legend {
      border: 2px #ccaaaa solid;
    }
    
    fieldset {
      border: 2px #af3333 solid;
      border-radius: 10px;
    }
    
    /* editable [pseudo]select (i.e. fieldsets with [class=elist]) */
    
    fieldset.elist {
      display: inline-block;
      position: relative;
      vertical-align: middle;
      overflow: visible;
      padding: 0;
      margin: 0;
      border: none;
    }
    
    fieldset.elist ul {
      position: absolute;
      width: 100%;
      max-height: 320px;
      padding: 0;
      margin: 0;
      overflow: hidden;
      background-color: transparent;
    }
    
    fieldset.elist:hover ul {
      background-color: #ffffff;
      border: 2px #af3333 solid;
      left: 2px;
      overflow: auto;
    }
    
    fieldset.elist ul > li {
      list-style-type: none;
      background-color: transparent;
    }
    
    fieldset.elist label {
      display: none;
      width: 100%;
    }
    
    fieldset.elist input[type="text"] {
      width: 100%;
      height: 30px;
      line-height: 30px;
      border: none;
      background-color: transparent;
      border-radius: 0;
    }
    
    fieldset.elist > legend {
      display: block;
      margin: 0;
      padding: 0 0 0 5px;
      position: absolute;
      width: 100%;
      cursor: default;
      background-color: #ccffcc;
      line-height: 30px;
      font-style: italic;
    }
    
    fieldset.elist:hover > legend {
      position: relative;
      overflow: hidden;
    }
    
    fieldset.elist > legend:after {
      width: 20px;
      content: "2335";
      float: right;
      text-align: center;
      border-left: 2px #cccccc solid;
      font-style: normal;
      cursor: default;
    }
    
    fieldset.elist:hover > legend:after {
      background-color: #99ff99;
    }
    
    fieldset.elist ul input[type="radio"] {
      display: none;
    }
    
    fieldset.elist input[type="radio"]:checked ~ label {
      display: block;
      width: 292px;
      background-color: #ffffff;
    }
    
    fieldset.elist:hover input[type="radio"]:checked ~ label {
      width: 100%;
    }
    
    fieldset.elist:hover label {
      display: block;
      height: 100%;
    }
    
    fieldset.elist label:hover {
      background-color: #3333ff !important;
    }
    
    fieldset.elist:hover input[type="radio"]:checked ~ label {
      background-color: #aaaaaa;
    }
    
    </style>
    
    </head>
    <body>
    
    <form method="get" action="test.php">
    
    <fieldset>
        <legend>order a t-shirt</legend>
        <p>write your name (simple textbox): <input type="text" /></p>
        <p>choose your size (simple select):
        <select>
            <option value="s">small</option>
            <option value="m">medium</option>
            <option value="l">large</option>
            <option value="xl">extra large</option>
        </select></p>
        <div>what address do you want to use? (editable pseudoselect)
        <fieldset class="elist">
            <legend>address&hellip;</legend>
            <ul>
                <li><input type="radio" value="1" id="address-switch_1" checked /><label for="address-switch_1"><input type="text" value="19 quaker ridge rd. bethel ct 06801" /></label></li>
                <li><input type="radio" value="2" id="address-switch_2" /><label for="address-switch_2"><input type="text" value="1000 coney island ave. brooklyn ny 11230" /></label></li>
                <li><input type="radio" value="3" id="address-switch_3" /><label for="address-switch_3"><input type="text" value="2962 dunedin cv. germantown tn 38138" /></label></li>
                <li><input type="radio" value="4" id="address-switch_4" /><label for="address-switch_4"><input type="text" value="915 e 7th st. apt 6l. brooklyn ny 11230" /></label></li>
            </ul>
        </fieldset>
        </div>
        <p>write a comment:<br />
        <textarea></textarea></p>
        <p><input type="reset" value="reset" /> <input type="submit" value="send!" /></p>
    </fieldset>
    
    </form>
    
    </body>
    </html>
    

    上篇:<option>

    下篇:<form>