Can I nest a <button> element inside an <a> using HTML5?
I am doing the following:
<a href="www.stackoverflow.com">
<button disabled="disabled" >ABC</button>
</a>
This works good but I get a HTML5 validation error that says "Element 'button' must not be nested within element 'a button'.
Can anyone give me advice on what I should do?
No , it isn't valid HTML5 according to the HTML5 Spec Document from W3C:
Content model: Transparent, but there must be no interactive content descendant.
The a element may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (eg buttons or other links).
In other words, you can nest any elements inside an <a>
except the following:
<a>
<audio>
(if the controls attribute is present)
<button>
<details>
<embed>
<iframe>
<img>
(if the usemap attribute is present)
<input>
(if the type attribute is not in the hidden state)
<keygen>
<label>
<menu>
(if the type attribute is in the toolbar state)
<object>
(if the usemap attribute is present)
<select>
<textarea>
<video>
(if the controls attribute is present)
If you are trying to have a button that links to somewhere, wrap that button inside a <form>
tag as such:
<form style="display: inline" action="http://example.com/" method="get">
<button>Visit Website</button>
</form>
However, if your <button>
tag is styled using CSS and doesn't look like the system's widget... Do yourself a favor, create a new class for your <a>
tag and style it the same way.
如果你使用的是Bootstrap 3 ,这个效果很好
<a href="#" class="btn btn-primary btn-lg active" role="button">Primary link</a>
<a href="#" class="btn btn-default btn-lg active" role="button">Link</a>
No.
The following solution relies on JavaScript.
<button type="button" onclick="location.href='http://www.stackoverflow.com'">ABC</button>
If the button is to be placed inside an existing <form>
with method="post"
, then ensure the button has the attribute type="button"
otherwise the button will submit the POST operation. In this way you can have a <form>
that contains a mixture of GET and POST operation buttons.
上一篇: 我如何让一个div浮到它的容器底部?