html - ListView 内的 Bootstrap 按钮不起作用

标签 html twitter-bootstrap twitter-bootstrap-3

我试图在 bootstrap 的 ListView 中获取一个按钮以访问两个不同的链接,并且它始终遵循 ListView 链接,而不是按钮链接(如果我按下按钮)。我如何实现这一目标?

<a href="foo.html" class="list-group-item">
    <p>This links somewhere different than the button</p>
    <button href="bar.html" class="btn btn-success">Example</button>
</a>

最佳答案

按原样使用 Bootstrap 似乎不容易完成,但一些调整可以让我们得到我们想要的东西。主要问题是嵌套 anchor 并不是真正有效的 HTML。然而,我们可以通过将一个链接绝对定位在另一个链接之上来达到同样的效果。

看看这个 JS Bin:

http://jsbin.com/febivi/2/

总结:

将一个新类添加到将定义我们新容器的列表组:

  <ul class="list-group action-list-group">
    <li class="list-group-item">
      <a class="list-group-link" href="http://stackoverflow.com">Cras justo odio</a>
      <a class="btn btn-sm btn-default" href="http://google.com">Go</a>
    </li>
    <li class="list-group-item">
      <a class="list-group-link" href="http://stackoverflow.com">Cras justo odio</a>
      <a class="btn btn-sm btn-default" href="http://google.com">Go</a>
    </li>  
  </ul>

主要链接是“.list-group-link”,次要链接是“.btn”操作。

接下来我们添加一些 CSS 来为列表组项目的内部操作列表组添加样式:

.action-list-group {
  position: relative;
}

/* remove the list group padding since our nested anchor tag will now have it */
.action-list-group .list-group-item {
  padding: 0;
}

.action-list-group .list-group-item > a.list-group-link {
  display: block;
  /* inherit from .list-group-item */
  padding: 10px 15px;
  color: #555;
}

/* re-add the link styling */
.action-list-group .list-group-item > a.list-group-link:hover {
  background: #f5f5f5;
  text-decoration: none;
}

.action-list-group .btn {
  position: absolute;
  right: 5px;
  margin-top: 5px;
  top: 0;
}

如果您使用的是 Sass version of Bootstrap,那么在它说“继承”的地方您可以使用 sass 的 @include@extend 来包含与 Bootstrap 的 .list-group-item > a 相同的样式。

关于html - ListView 内的 Bootstrap 按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26596927/

相关文章:

javascript - 在缺乏 CSS 技能的情况下创建可调整大小的网站 - CSS 形状

html - 是否可以使用 thead 和 tbody 制作带有顶部和左侧标题的表格?

javascript - 修复 iOS/iPad 2 中的溢出内容滚动

javascript - 不能将 onChange 与 react-bootstrap ToggleButtonGroup 一起使用

html - 我如何控制导航栏的溢出

html - Bootstrap 3 "fluid"页脚

html - 如何防止 Webpack 进程从输入标记中删除类型 ="text"

CSS:行列边框未显示

javascript - AngularJS + SPA - 更改布局

html - Bootbox:消息溢出