如何创建像链接一样的HTML按钮?因此,单击该按钮可将用户重定向到页面。

我希望它是可访问的,并且在URL中使用最少的额外字符或参数。


当前回答

从HTML5开始,按钮支持formaction属性。最重要的是,不需要JavaScript或诡计。

<表单><button formaction=“http://stackoverflow.com“>转到堆栈溢出!</button></form>

注意事项

必须由<form>标记包围。<button>类型必须是“submit”(或未指定)-我无法使用“button”类型。这引出了以下问题。重写表单中的默认操作。换句话说,如果在另一个表单中执行此操作,将导致冲突。

参考:formaction

浏览器支持:<button>:button元素

其他回答

七种方法:

使用window.location.href='URL'使用window.location.replace('URL')使用window.location=“URL”使用window.open(“URL”)使用window.location.assign('URL')使用HTML表单使用HTML定位标记

<!-- 使用window.location.href='URL'--><button onclick='window.location.href=“https://stackoverflow.com"'>单击我</按钮><!-- 使用window.location.replace('URL')--><button onclick='窗口位置替换(“https://stackoverflow.com")'>单击我</按钮><!-- 使用window.location=“URL”--><button onclick='window.location=“https://stackoverflow.com"'>单击我</按钮><!-- 使用window.open('URL')--><button onclick='窗口打开(“https://stackoverflow.com“,”_self“,”“,”)'>单击我</按钮><!-- 使用window.location.assign('URL')--><button onclick='窗口位置分配(“http://www.stackoverflow.com")'>单击我</按钮><!-- 使用HTML表单--><表单操作='https://stackoverflow.com'方法='get'><input-type='submit'value='Click-Me'/></form><!-- 使用HTML定位标记--><a href='https://stackoverflow.com'><button>单击我</button></a>

<button onclick=“location.href='http://www.example.com'“type=”按钮“>www.example.com</button>

请注意,type=“button”属性很重要,因为它缺少的默认值是SubmitButton状态。

如果要创建用于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;
}

在<button></button>上使用<a></a>属性回答的人很有帮助。

但最近,我在<form></form>中使用链接时遇到了一个问题。

该按钮现在被视为提交按钮(HTML5)。我已经尝试了一种方法,并找到了这种方法。

创建一个CSS样式按钮,如下所示:

.btn-style {
    border: solid 1px #0088cc;
    border-radius: 6px;
    moz-border-radius: 6px;
    -webkit-box-shadow: 0px 0px 2px rgba(0, 0, 0, 1.0);
    -moz-box-shadow: 0px 0px 2px rgba(0, 0, 0, 1.0);
    box-shadow: 0px 0px 2px rgba(0, 0, 0, 1.0);
    font-size: 18px;
    color: #696869;
    padding: 1px 17px;
    background: #eeeeee;
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%, #eeeeee), color-stop(49%, #eeeeee), color-stop(72%, #cccccc), color-stop(100%, #eeeeee));
    background: -moz-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
    background: -webkit-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
    background: -o-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
    background: -ms-linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
    background: linear-gradient(top, #eeeeee 0%, #eeeeee 49%, #cccccc 72%, #eeeeee 100%);
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#eeeeee', endColorstr='#eeeeee', GradientType=0);
}

或者在这里创建一个新的:CSS按钮生成器

然后用一个以您所创建的CSS样式命名的类标记创建链接:

<a href='link.php' class='btn-style'>Link</a>

这是一把小提琴:

JSFiddle公司

我知道已经提交了很多答案,但似乎没有一个能真正解决问题。以下是我对解决方案的看法:

使用OP开头的<form method=“get”>方法。这非常有效,但有时会附加一个?到URL。这个是主要问题。此解决方案在不启用JavaScript的情况下工作。回退将添加?到URL的结尾。如果启用了JavaScript,那么您可以使用jQuery/JavaScript来处理以下链接,这样吗?不会最终附加到URL。对于极少数未启用JavaScript的用户,它将无缝地回退到<form>方法。JavaScript代码使用事件委托,因此您可以在<form>或<button>存在之前附加一个事件监听器。我在这个例子中使用jQuery,因为它既快速又简单,但也可以用“香草”JavaScript实现。JavaScript代码防止发生默认操作,然后遵循<form>action属性中给出的链接。

JSBin示例(代码段不能跟随链接)

//使用“link”类侦听文档中某个元素的任何单击$(document).on('click','.link',函数(e){//阻止默认操作(例如提交表单)e.预防违约();//获取表单中指定的URLvar url=e.target.parentElement.action;window.location=url;});<!DOCTYPE html><html><head><script src=“https://code.jquery.com/jquery-1.11.1.min.js“></script><meta charset=“utf-8”><title>表单按钮作为链接</title></head><body><!-- 将“action”设置为按钮要转到的URL--><form method=“get”action=“http://stackoverflow.com/questions/2906582/how-to-create-an-html-button-that-acts-like-a-link"><!-- 将类“link”添加到事件侦听器-->的按钮<button type=“submit”class=“link”role=“link”>链接</button></form></body></html>