如何在 HTML 中使用 step 属性?

htmlweb developmentfront end technology更新于 2025/12/29 4:28:17

HTML 输入类型的 step 属性设置合法的数字间隔。step 是数字步长,例如 0、4、8、12、16 等。step 属性可以与 max 和 min 属性一起使用,以创建合法值的范围。

示例

您可以尝试运行以下代码,使用输入的 step 属性创建一个数字步长。这里我们创建了步骤 4,因此有效输入为 4、8、12、16 等。输入除此以外的数字并点击"提交"按钮后,将显示以下消息:"请输入有效值"。它还会显示两个最接近的值。

<!DOCTYPE html>
<html>
   <head>
      <title>HTML input step attribute</title>
   </head>
   <body>
     <form action = "/cgi-bin/hello_get.cgi" method = "get">
        <input type = "number" name = "points" step = "4"><br>
        <input type = "submit" value = "Submit">
     </form>
   </body>
</html>

相关文章


有用资源