type ='button'还能做什么吗?

问题描述 投票:0回答:1

我刚注意到这一点,很好奇。我有两个带有onClick事件的<span>,它们的作用基本相同。一个有type='button',但另一个没有。他们俩的行为完全一样。

除了使type='button'更清楚是什么以外,是否还有其他必要?

即使有和没有,标准的button元素的行为也似乎完全相同。

<button onclick="alert('Hello world!')">Click Me!</button>

&&

<button type="button" onclick="alert('Hello world!')">Click Me!</button>

html dom
1个回答
0
投票

您可以制作表格以查看不同之处:

<form onsubmit="alert('submited...')">
  <button>Click me</button>
</form>

<form onsubmit="alert('submited...')">
  <button type="button">Click me</button>
</form>

第一个示例中没有button属性的type标签,它与submit按钮(<button type="submit">Click me</button>)相关,而第二个按钮则不相关。

© www.soinside.com 2019 - 2024. All rights reserved.