首页 > 解决方案 > 如何在 HTML5 中创建一个带有链接的按钮?

问题描述

我尝试了一种方法来做到这一点:
<button href='www.google.com'>Google</button>.
但这没有用。谁能找到解决方案?我想知道如何做到这一点。

标签: html

解决方案


将按钮嵌套在锚点中:

<a href='https://www.google.com'><button>Google</button></a>

或者将其包装在一个表单中(符合HTML 5 Specs):

<form style="display: inline" action="https://www.google.com" method="get">
  <button>Google</button>
</form>

或者,您可以使用 JS 附加click更改的事件侦听器window.location

document.querySelector('button').onclick=function(){
  window.location = this.getAttribute('href');
}
<button href='https://www.google.com'>Google</button>


推荐阅读