将 HTML 按钮转换为 Link 的最佳方法

Best way to Convert HTML Button to a Link

我有以下代码,其中有一个带有箭头 hack 的按钮。唯一的问题是我想让它在状态栏上显示 link。一键不实现。如何将其转换为 a href link 并仍然能够保留该箭头?这是我的代码:

JSFIDDLE

HTML:

<button type="button" class="btn-lg btn-default my-button my-button-active">My Button</button>

CSS:

.my-button {
    color: #fff ;
    background-color: #444346;
    font-size:15px;
    padding: 20px 0px;
    border:none;
    margin-right:20px;
    position: relative;
    outline:0;
    width:31%;
}


.my-button-active:after {
    content:'';
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -10px;
    width: 0;
    height: 0;
    border-top: solid 7px #444346;
    border-left: solid 7px transparent;
    border-right: solid 7px transparent;
}

.my-button-active:hover:after {
    content:'';
    position: absolute;
    top: 100%;
    left: 50%;
    margin-left: -10px;
    width: 0;
    height: 0;
    border-top: solid 7px #e6e6e6;
    border-left: solid 7px transparent;
    border-right: solid 7px transparent;
}

<button> 标签更改为 <a> 标签后,关键是应用 display: block;,因为 <a> 标签主要是内联元素,不能有填充和边距。

只需将您的按钮更改为锚点并调整您的 css。

div {
  margin: 30px;
  
}

a.my-button {
  color: #fff ;
  background-color: #444346;
  font-size:15px;
  padding: 20px 0px;
  border:none;
  margin-right:20px;
  position: relative;
  outline:0;
  width:31%;
  display: inline-block;
  text-align: center;
}

.my-button:hover {
  text-decoration: none;
}


.my-button-active:after {
  content:'';
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -10px;
  width: 0;
  height: 0;
  border-top: solid 7px #444346;
  border-left: solid 7px transparent;
  border-right: solid 7px transparent;
}

.my-button-active:hover:after {
  content:'';
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -10px;
  width: 0;
  height: 0;
  border-top: solid 7px #e6e6e6;
  border-left: solid 7px transparent;
  border-right: solid 7px transparent;

}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<div>
  <a href="#" type="button" class="btn-lg btn-default my-button my-button-active">My Button that links to some other page</a>
</div>

将您的 html 更改为锚标签:

<a class="btn-lg btn-default my-button my-button-active" href="">My Button that links to some other page</a>

将您的 .my-button 选择器替换为:

.my-button {
    display: block;
    position: relative;
    color: #fff;
    background-color: #444346;
    font-size:15px;
    padding: 20px 0;
    text-align: center;
    border: none;
    margin-right: 20px;
    outline: 0;
    width: 31%;
}

很简单。只需将按钮转换为 a 并将 display: block;display: inline-block; 添加到 class .my-button。

Fiddle link: http://jsfiddle.net/samirkumardas/60n0ruyj/2/