我刚注意到这一点,很好奇。我有两个带有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>
您可以制作表格以查看不同之处:
<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>
)相关,而第二个按钮则不相关。