如何创建像链接一样的HTML按钮?因此,单击该按钮可将用户重定向到页面。
我希望它是可访问的,并且在URL中使用最少的额外字符或参数。
如何创建像链接一样的HTML按钮?因此,单击该按钮可将用户重定向到页面。
我希望它是可访问的,并且在URL中使用最少的额外字符或参数。
当前回答
如果你需要的是它看起来像一个按钮,重点放在渐变图像上,你可以这样做:
<a href="www.yourlink.com" class="btn btn-gradient"><i class="fa fa-home"> Button Text</i></a>
其他回答
它实际上非常简单,不使用任何表单元素。您可以只使用<a>标签,里面有一个按钮:)。
这样地:
<a href="http://www.google.com" target="_parent"><button>Click me !</button></a>
它会将href加载到同一页面中。想要新页面吗?只需使用target=“_blank”。
EDIT
几年后,虽然我的解决方案仍然有效,但请记住,您可以使用大量CSS来使它看起来像您想要的那样。这只是一条捷径。
在浏览器控制台中键入window.location并按Enter键。然后,您可以清楚地知道位置包含:
hash: ""
host: "stackoverflow.com"
hostname: "stackoverflow.com"
href: "https://stackoverflow.com/questions/2906582/how-to-create-an-html-button-
that-acts-like-a-link"
origin: "https://stackoverflow.com"
pathname: "/questions/2906582/how-to-create-an-html-button-that-acts-like-a-link"
port: ""
protocol: "https:"
您可以从这里设置任何值。
因此,对于重定向另一个页面,可以使用链接设置href值。
window.location.href = your link
在您的案例中:
<button onclick="window.location.href='www.google.com'">Google</button>
这里的所有解决方案都不适合我,所以在尝试了所有解决方案之后,我得到了一些微不足道的东西:仅HTML(无表单提交),无CSS,无JS:
<a href="/link"><button>Link</button></a>
Bootstrap方法也适用于Bulma。
<link rel=“stylesheet”href=“https://cdn.jsdelivr.net/npm/bulma@0.9.2/css/bulma.min.css“><a href=“https://www.stackoverflow.com“class=”button is primary“>堆栈溢出</a>
如果要创建用于URL的按钮,请为锚点创建按钮类。
a.button {
background-color: #999999;
color: #FFFFFF !important;
cursor: pointer;
display: inline-block;
font-weight: bold;
padding: 5px 8px;
text-align: center;
-webkit-border-radius: 5px;
border-radius: 5px;
}
.button:hover {
text-decoration: none;
}