html - 如何在 HTML5 中创建一个带有链接的按钮?
问题描述
我尝试了一种方法来做到这一点:
<button href='www.google.com'>Google</button>
.
但这没有用。谁能找到解决方案?我想知道如何做到这一点。
解决方案
将按钮嵌套在锚点中:
<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>
推荐阅读
- javascript - 如何修复无法在组件上调用 setState,但它可能会在使用 React Js 的应用程序中出现错误
- r - R data.table 保留组的第一个非NA值直到组结束
- python - 如何解决 wget 下载的 403 错误
- react-native - 没有在 FlatList 上应用borderRadius
- python - 将年份和日期转换为 pandas 时间戳
- vb.net - csv 中的数据未正确对齐
- php - 以整数形式返回时间差
- postgresql - 自动增量不适用于序列类型的 ID 列
- python - 比较不同服务器中两个相似表之间的数据
- amazon-web-services - 将 IAM 角色从一个 EC2 传递到另一个 Ec2