HTML基础(3)

表单元素

表单是一个包含表单元素的区域,表单元素是允许用户在表单中输入内容,比如:文本域(textarea)、下拉列表、单选框(radio-buttons)、复选框(checkboxes)等等.所有的表单内容,都要写在form标签里面.

<form>
 所有的表单内容,都要写在form标签里面
</form>

input标签

input标签有一个type属性, 这个属性有很多类型的取值, 取值的不同就决定了input标签的功能和外观不同.

文本域

  • 明文输入框:用户可以在输入框内输入内容
    账号:<input type="text"/>
  • 暗文输入框:用户可以在输入框内输入内容
    密码:<input type="password"/>
  • 给输入框设置默认值
    账号:<input type="text" value="123"/>
    密码:<input type="password" value="123"/>
    <input type="text" placeholder="坚持就是胜利">
    placeholder 属性提供可描述输入字段预期值的提示信息,该提示会在输入字段为空时显示,并会在字段获得焦点时消失
  • 规定输入字段中的字符的最大长度
    账号: <input type="text" name="fullname" maxlength="8" />

多行文本域

<textarea cols="30" rows="10">默认</textarea>

  • cols属性表示columns“列”, 规定文本区内的可见宽度
  • rows属性表示rows“行”, 规定文本区内的可见行数
  • 默认情况下输入框是可以手动拉伸的,可以用css中的resize为none来固定

按钮

单选按钮(Radio Buttons)
<input type="radio" name="xingbie" /> 男
<input type="radio" name="xingbie" /> 女
<input type="radio" name="xingbie" /> 妖

notice:单选按钮,天生是不互斥的,如果想互斥,必须要有相同的name属性.

复选框(Checkboxes)
<input type="checkbox" name="aihao" checked="checked"/> 篮球
<input type="checkbox" name="aihao" checked="checked"/> 足球
<input type="checkbox" name="aihao"/> 棒球

notice:复选框,最好也是有相同的name(虽然他不需要互斥,但是也要有相同的name)

提交按钮(Submit Button)

定义提交按钮.提交按钮会把表单数据发送到action属性指定的页面

<input type="submit" />
notice:

  • 这个按钮不需要写value自动就有“提交”文字
  • 要想通过submit提交数据到服务器, 被提交的表单项都必须设置name属性
  • 默认明文传输(GET)不安全, 可以将method属性设置为POST改为非明文传输(学到Ajax再理解)
重置按钮(Reset Button)

定义重置按钮.点击之后会将重置按钮所在的表单中填写的内容清空.

<input type="reset">

label标签

label标签不会向用户呈现任何特殊效果.不过,它为鼠标用户改进了可用性.
方法一:

<!--给文本框添加绑定-->
<label for="account">账号:</label>
<input type="text" id="account" />
<!--给单选框添加绑定-->
<input type="radio" name="sex" id="man" /> <label for="man">男</label>
<!--给多选框添加绑定-->
<input type="checkbox" id="basketball" />
<label for="basketball">篮球</label>

表单元素要有一个id,然后label标签就有一个for属性,for属性和id相同就表示绑定了,所有表单元素都可以通过label绑定.
方法二:

<label>
账号:<input type="text">
</label>
<label >
密码:<input type="password">
</label>

直接用label标签包裹.

数据列表

作用:给输入框绑定待选项

datalist>
  <option>待选项内容</option>
</datalist>

如何给输入框绑定待选列表

  • 搞一个输入框
  • 搞一个datalist列表
  • 给datalist列表标签添加一个id
  • 给输入框添加一个list属性,将datalist的id对应的值赋值给list属性即可
请输入你的车型: <input type="text" list="cars">
<datalist id="cars">
    <option>奔驰</option>
    <option>宝马</option>
    <option>奥迪</option>
    <option>路虎</option>
    <option>宾利</option>
</datalist>
QQ截图20170810152328.png

下拉列表

作用: select标签和ul、ol、dl一样,都是组标签. 用于创建表单中的待选列表, 可以从选择某一个带选项

选择籍贯:
<select>
  <option selected="selected">北京</option>
  <option>河北</option>
  <option>河南</option>
  <option>山东</option>
  <option>山西</option>
  <option>湖北</option>
  <option>贵州</option>
</select>

给下拉列表添加分组

<select>
  <optgroup label="北京市">
      <option>海淀区</option>
      <option>昌平区</option>
      <option>朝阳区</option>
  </optgroup>
      <optgroup label="广州市">
      <option>天河区</option>
      <option>白云区</option>
  </optgroup>
</select>

notice:

  • 下拉列表不能输入内容,但是可以直接在列表中选择内容
  • 可以通过给option标签添加一个selected属性来默认指定列表的默认值
  • 可以通过给option包裹一层optgroup标签来给下拉列表的内容分类

fieldset标签

作用:可以给表单添加一个边框,legend标签可以为边框指定一个标题

<form>
        <fieldset>
        <legend>表单练习</legend>
        <label>
            账号:<input type="text"><br>
        </label>
        <label>
            密码:<input type="password"><br>
        </label>
        性别:
        <input type="radio" id="man" name="name">
        <label for="man">男</label>
        <input type="radio" id="girl" name="name">
        <label for="girl">女</label>
        <input type="radio" id="secrit" name="name" checked="checked">
        <label for="secrit">保密</label><br>
        爱好: 
        <input type="checkbox" id="basketball">
        <label for="basketball">男球</label>
        <input type="checkbox" id="baseball">
        <label for="baseball">足球</label>
        <input type="checkbox" id="xxoo" checked="checked">
        <label for="xxoo">足浴</label><br>
        <label for="introduce">个人简介</label>
        <textarea name="" id="introduce" cols="30" rows="10"></textarea><br>
        <label for="birthday">生日</label>
        <input type="date" id="birthday"><br>
        <label for="email">邮箱</label>
        <input type="email" id="email"><br>
        <label for="mobile">手机</label>
        <input type="number" id="mobile"><br>
        <label for="urle">域名</label>
        <input type="url" id="urle"><br>
        <label for="times">时间</label>
        <input type="date" id="times"><br>
        <label for="col">颜色</label>
        <input type="color" id="col"><br>
        <input type="submit" value="注册">&nbsp;&nbsp;&nbsp;
        <input type="reset" value="清空"><br>
    </fieldset>
</form>
QQ截图20170810154934.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • HTML标签解释大全 一、HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(D...
    米塔塔阅读 3,331评论 1 41
  • 引言 在日常开发Android中,很多时候会遇到和WebView打交道,对CSS HTML JS不是很清楚的话是完...
    张文靖同学阅读 2,885评论 0 11
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,009评论 19 139
  • 我已经三十几的人了,过去的三十年都像个小孩子一样,自私任性,对于爱情充满幻想,我以为只要我不顾一切,能得到我想要的...
    小妞勿勿阅读 311评论 1 1
  • 今早起来后脑勺特别疼 希望是欣欣向荣的一天 最后 真希望我下辈子是个不碍于面子的人
    假装勤奋的大宝阅读 147评论 0 0