📜  HTML |类型属性(1)

📅  最后修改于: 2023-12-03 15:31:16.211000             🧑  作者: Mango

HTML 类型属性

在 HTML 中,类型属性(type attribute)用于指定输入字段(input field)或按钮的类型。根据不同的类型,输入字段或按钮的外观和行为会有所不同。

常用类型属性

以下是常见的类型属性:

  • text:用于文本输入框(text input field),用户可以输入任意文本内容。
  • email:用于电子邮件地址输入框,确保用户输入的内容符合电子邮件地址的格式。
  • password:用于密码输入框,输入的文本内容会被隐藏。
  • number:用于数字输入框,只允许输入数字。
  • checkbox:用于复选框(checkbox),用户可以选中或取消选中。
  • radio:用于单选框(radio button),用户可以在多个选项中选择其中一个。
  • submit:用于提交表单(form submission)。
  • button:用于普通按钮(button),可以用 JavaScript 等脚本语言制定其行为。
示例

以下代码片段是一个简单的 HTML 表单,包含文本输入框、密码输入框、单选框、复选框和提交按钮:

<form>
  <label for="username">用户名:</label>
  <input type="text" id="username" name="username">

  <label for="password">密码:</label>
  <input type="password" id="password" name="password">

  <p>你喜欢的编程语言:</p>
  <label>
    <input type="radio" name="language" value="JavaScript">
    JavaScript
  </label>
  <label>
    <input type="radio" name="language" value="Python">
    Python
  </label>

  <p>你使用过的操作系统:</p>
  <label>
    <input type="checkbox" name="os" value="Windows">
    Windows
  </label>
  <label>
    <input type="checkbox" name="os" value="macOS">
    macOS
  </label>
  <label>
    <input type="checkbox" name="os" value="Linux">
    Linux
  </label>

  <button type="submit">提交</button>
</form>
总结

类型属性是 HTML 中用于指定输入字段或按钮类型的一种属性。根据不同的类型,输入字段或按钮的外观和行为会有所不同。常见的类型属性包括 text、email、password、number、checkbox、radio、submit 等。