Html <输入类型=按钮> vs <按钮>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25436145/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
<input type=button> vs <button>
提问by Strawberry Farmer
I'm a little confused. What is the difference between these. Please don't reference really old postings. I notice that accessing some of the styles are different inline in html as well as in style sheets.
我有点困惑。这些有什么区别。请不要引用非常旧的帖子。我注意到在 html 和样式表中访问某些样式是不同的。
<input type=button>
vs
<button>
I guess I'm wondering which one will out live which? or which is the best when taking into account ease of compatibility between all the general technologies that go into website creation? aka. which is going to cause the least amount of trouble
我想我想知道哪一个会住哪一个?或者考虑到网站创建中所有通用技术之间的兼容性,哪个是最好的?又名。这将导致最少的麻烦
回答by Ben Roux
Unlike <input>
tags, <button>
's can contain other html elements as their labels. <input type="button">
can only accept a string as its label text (css styles not withstanding).
与<input>
标签不同,<button>
's 可以包含其他 html 元素作为它们的标签。<input type="button">
只能接受一个字符串作为其标签文本(css 样式不支持)。
Additionally, the <button>
element accepts a wide range of uncommon but useful attributes regarding multiple forms and click actions. See the MDN page for more details.
此外,该<button>
元素接受大量关于多个表单和单击操作的不常见但有用的属性。有关更多详细信息,请参阅 MDN 页面。
As for one "out living" the other, the HTML standard is remarkably backwards compatible. Humanity will put men on Mars before either is eliminated from the HTML standard.
至于一个“外向生活”另一个,HTML 标准是非常向后兼容的。在 HTML 标准中的任何一个被淘汰之前,人类都会将人类送上火星。
回答by Jijin M
Inside a <button>
element you can put content, like text or images.
eg: <button type="button" onclick="alert('Hello world!')">Click Me!</button>
在<button>
元素中,您可以放置内容,例如文本或图像。例如:<button type="button" onclick="alert('Hello world!')">Click Me!</button>
If you use the <button>
element in an HTML form, different browsers may submit different values. So always use <input type="button">
to create buttons in an HTML form.
如果您<button>
在 HTML 表单中使用该元素,不同的浏览器可能会提交不同的值。所以总是用来<input type="button">
在 HTML 表单中创建按钮。
回答by AKNair
input type=button
The tag is the easiest way to submit a form. When a customer clicks on the button, it submits automatically. You don't need to add any scripts, the browsers know to submit the form when a submit INPUT tag is clicked.
input type=button
标签是提交表单最简单的方式。当客户点击按钮时,它会自动提交。您不需要添加任何脚本,浏览器知道在单击提交 INPUT 标签时提交表单。
The problem is that this button is very ugly and plain. You can't add images to it. You can style it just like any other element, but it can still feel like an ugly button.
问题是这个按钮非常丑陋和简单。您无法向其中添加图像。你可以像任何其他元素一样设置它的样式,但它仍然感觉像一个丑陋的按钮。
Use the INPUT method when your form has to be accesible even in browsers that have JavaScript turned off.
即使在关闭 JavaScript 的浏览器中也必须可以访问表单时,请使用 INPUT 方法。
button
按钮
The BUTTON element offers more options for submiting forms. You can put anything inside a BUTTON element and turn it into a submit button. Most commonly people use images and text. But you could create a DIV and make that entire thing a submit button if you wanted to.
BUTTON 元素为提交表单提供了更多选项。您可以将任何内容放入 BUTTON 元素并将其转换为提交按钮。人们最常使用图像和文本。但是,如果您愿意,您可以创建一个 DIV 并将整个内容设为提交按钮。
The biggest drawback to the BUTTON element is that it doesn't automatically submit the form. This means there needs to be some type of script to activate it. And so it is less accessible than the INPUT method. Any user who doesn't have JavaScript turned on won't be able to submit a form with only aBUTTON element to submit it.
BUTTON 元素的最大缺点是它不会自动提交表单。这意味着需要某种类型的脚本来激活它。因此它比 INPUT 方法更不易访问。任何没有打开 JavaScript 的用户将无法提交只有一个 BUTTON 元素的表单来提交它。
Use the BUTTON method on forms that are not as critical. Also, this is a great way to add additional submission options within one form.
在不那么重要的表单上使用 BUTTON 方法。此外,这是在一个表单中添加其他提交选项的好方法。
回答by Jatin
Use <button>
from input element if you want to create button in a form.
<button>
如果要在表单中创建按钮,请使用from input 元素。
And use button tag if you want to create button for an action.
如果要为操作创建按钮,请使用按钮标签。
More Info: Difference between <input type='submit' /> and <button type='submit'>text</button>
更多信息:<input type='submit' /> 和 <button type='submit'>text</button> 之间的区别
回答by alexndm
depends where you want to use it. input should be inside form, where button can be used anywhere.
取决于你想在哪里使用它。输入应该在表单内,按钮可以在任何地方使用。