HTMLInputElement.stepDown([n]) method decrements the value of a numeric type of <input> element by the value of the step attribute or up to n multiples of the step attribute if a number is passed as the parameter. The method, when invoked, decrements the by ( step * n), where n defaults to 1 if not specified, and step defaults to the default value for step if not specified.

Valid on all numeric, date, and time input types that support the step attribute, including date , month , week , time , datetime-local , number ,和 range .

Given <input id="myTime" type="time" max="17:00" step="900" value="17:00"> , invoking myTime.step(3) will change the value to 16:15, decrementing the time by 3 * 900 , or 45 minutes. myTime.step() , with no parameter, would have resulted in 16:45 , as n 默认为 1 .

<!--  decrements by intervals of 900 seconds (15 minute) -->
<input type="time" max="17:00" step="900">
<!-- decrements by intervals of 7 days (one week) -->
<input type="date" max="2019-12-25" step="7">
<!-- decrements by intervals of 12 months (one year) -->
<input type="month" max="2019-12" step="12">
					

The method, when invoked, changes the form control's value by the value given in the step attribute, multiplied by the parameter, within the constraints set within the form control. The default value for the parameter, if not is passed, is 1. The method will not cause the value to go below the min value set or defy the constraints set by the step attribute. A negative value for n will increment the value, but will not increment beyond the max 值。

If the value before invoking the stepDown() method is invalid, for example, if it doesn't match the constraints set by the step attribute, invoking the stepDown() method will return a value that does match the form controls constraints.

If the form control is non time, date, or numeric in nature, and therefore does not support the step attribute (see the list of supported input types in the the table above), or if the step value is set to any InvalidStateError exception is thrown.

HTMLInputElement.stepDown()
递减 by ( step * n), where n defaults to 1 if not specified. Throws an INVALID_STATE_ERR exception:
  • if the method is not applicable to for the current type 值,
  • if the element has no step 值,
  • cannot be converted to a number,
  • if the resulting value is above the max or below the min .

句法

element.stepDown( [ stepDecrement ] );
					

参数

stepDecrement
可选 stepDecrement paremeter is a numeric value.  If no parameter is passed, stepDecrement 默认为 1。
If the value is a float, the value will increment as if Math.floor(stepDecrement) was passed. If the value is negative, the value will be incremented instead of decremented.

范例

Click the button in this example to increment the number input type:

HTML

<p>
  <label>Enter a number between 0 and 400 that is divisible by 5:
   <input type="number" step="5" id="theNumber" min="0" max="400">
  </label>
</p>
<p>
  <label>Enter how many values of step you would like to increment by or leave it blank:
   <input type="number" step="1" id="decrementer" min="-2" max="15">
  </label>
</p>
<input type="button" value="Decrement" id="theButton">
						

JavaScript

/* make the button call the function */
let button = document.getElementById('theButton');
button.addEventListener('click', function() {
  stepondown();}
);
function stepondown() {
  let input = document.getElementById('theNumber');
  let val = document.getElementById('decrementer').value;
  if (val) {  /* increment with a parameter */
    input.stepDown(val);
  } else {    /* or without a parameter. Try it with 0, 5, -2, etc. */
    input.stepDown();
  }
}
						

CSS

input:invalid {
  border: red solid 3px;
}
						

结果

Note if you don't pass a parameter to the stepDown() method, it defaults to 1. Any other value is a multiplier of the step attribute value, which in this case is 5. If we pass 4 as the stepDecrement, the input will stepDown by 4 * 5 ,或 20 . If the parameter is 0, the number will not be decremented. The stepDown() method will not allow the input to go out of range, in this case stopping when it reaches 0 and rounding down and floats that are passed as a parameter.

Try setting the step decrementer to 1.2 . What happens when you invoke the method?

Try setting the value to 44 , which is not valid. What happens when you invoke the method?

规范

规范 状态 注释
HTML 实时标准
The definition of 'stepDown()' in that specification.
实时标准
HTML 5.1
The definition of 'stepDown()' in that specification.
推荐
HTML5
The definition of 'stepDown()' in that specification.
推荐 初始定义

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
stepDown Chrome Yes Edge 12 Firefox 16 注意事项 Disabled
16 注意事项 Disabled
Experimental, and without specific UI. There are still differences with the latest spec: bug 835773 .
Disabled ). To change preferences in Firefox, visit about:config.
IE ? Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android Yes Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改:
  1. HTMLInputElement
  2. 特性
    1. labels
    2. multiple
    3. webkitdirectory
    4. webkitEntries
  3. 方法
    1. mozGetFileNameArray()
    2. mozSetFileNameArray()
    3. select()
    4. setRangeText()
    5. setSelectionRange()
    6. stepDown()
    7. stepUp()
  4. 事件
    1. 无效
    2. search
  5. 继承:
    1. HTMLElement
    2. 元素
    3. 节点
    4. EventTarget
  6. HTML DOM 相关页面
    1. BeforeUnloadEvent
    2. DOMStringMap
    3. ErrorEvent
    4. GlobalEventHandlers
    5. HTMLAnchorElement
    6. HTMLAreaElement
    7. HTMLAudioElement
    8. HTMLBRElement
    9. HTMLBaseElement
    10. HTMLBaseFontElement
    11. HTMLBodyElement
    12. HTMLButtonElement
    13. HTMLCanvasElement
    14. HTMLContentElement
    15. HTMLDListElement
    16. HTMLDataElement
    17. HTMLDataListElement
    18. HTMLDialogElement
    19. HTMLDivElement
    20. HTMLDocument
    21. HTMLElement
    22. HTMLEmbedElement
    23. HTMLFieldSetElement
    24. HTMLFormControlsCollection
    25. HTMLFormElement
    26. HTMLFrameSetElement
    27. HTMLHRElement
    28. HTMLHeadElement
    29. HTMLHeadingElement
    30. HTMLHtmlElement
    31. HTMLIFrameElement
    32. HTMLImageElement
    33. HTMLIsIndexElement
    34. HTMLKeygenElement
    35. HTMLLIElement
    36. HTMLLabelElement
    37. HTMLLegendElement
    38. HTMLLinkElement
    39. HTMLMapElement
    40. HTMLMediaElement
    41. HTMLMetaElement
    42. HTMLMeterElement
    43. HTMLModElement
    44. HTMLOListElement
    45. HTMLObjectElement
    46. HTMLOptGroupElement
    47. HTMLOptionElement
    48. HTMLOptionsCollection
    49. HTMLOutputElement
    50. HTMLParagraphElement
    51. HTMLParamElement
    52. HTMLPictureElement
    53. HTMLPreElement
    54. HTMLProgressElement
    55. HTMLQuoteElement
    56. HTMLScriptElement
    57. HTMLSelectElement
    58. HTMLShadowElement
    59. HTMLSourceElement
    60. HTMLSpanElement
    61. HTMLStyleElement
    62. HTMLTableCaptionElement
    63. HTMLTableCellElement
    64. HTMLTableColElement
    65. HTMLTableDataCellElement
    66. HTMLTableElement
    67. HTMLTableHeaderCellElement
    68. HTMLTableRowElement
    69. HTMLTableSectionElement
    70. HTMLTemplateElement
    71. HTMLTextAreaElement
    72. HTMLTimeElement
    73. HTMLTitleElement
    74. HTMLTrackElement
    75. HTMLUListElement
    76. HTMLUnknownElement
    77. HTMLVideoElement
    78. HashChangeEvent
    79. 历史
    80. ImageData
    81. 定位
    82. MessageChannel
    83. MessageEvent
    84. MessagePort
    85. Navigator
    86. NavigatorGeolocation
    87. NavigatorID
    88. NavigatorLanguage
    89. NavigatorOnLine
    90. NavigatorPlugins
    91. PageTransitionEvent
    92. Plugin
    93. PluginArray
    94. PopStateEvent
    95. PortCollection
    96. PromiseRejectionEvent
    97. RadioNodeList
    98. Transferable
    99. ValidityState
    100. Window
    101. WindowBase64
    102. WindowEventHandlers
    103. WindowTimers