我正在try 添加一个链接在accordion-button,并使只有箭头可点击折叠

我添加了以下css,以使箭头仅可点击.但努力使accordion-button人中的联系发挥作用.我想让用户go 按钮的位置,而不是折叠.

.accordion-button {
    pointer-events: none;
}

.accordion-button::after {
    pointer-events: auto;
}

这就是我所做的.我如何才能使箭头只在同一时间可点击,允许accordion-button中的href可点击,并将用户带到链接?

.accordion-button {
    pointer-events: none;
}

.accordion-button::after {
    pointer-events: auto;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
        
    
<div class="accordion" id="accordionExample">
  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
        <a href="/" class="btn btn-sm btn-link">Accordion Item #1</a>
      </button>
    </h2>
    <div id="collapseOne" class="accordion-collapse collapse show" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
        <a href="/" class="btn btn-sm btn-link">Accordion Item #2</a>
      </button>
    </h2>
    <div id="collapseTwo" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
        <a href="/" class="btn btn-sm btn-link">Accordion Item #3</a>
      </button>
    </h2>
    <div id="collapseThree" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
</div>

推荐答案

不要在纽扣中嵌套锚.这只是一个坏主意,而且是无效的HTML.Buttonsanchors有不同的用途--按钮做动作,锚(链接)做导航.这两家公司都允许使用符合HTML规范的内容.如果你嵌套他们,你就会迷惑你的用户,并造成可访问性问题.

您也不需要为手风琴标题使用标题元素.如果您想要该文档 struct ,可以用它们来包装您的链接.

  • 将折叠页眉用作项目居中对齐的弹性容器
  • 将 colored颜色 和边框应用于手风琴标题
  • 移除手风琴标题按钮上的 colored颜色 和阴影
  • 或者,通过将按钮包装在div中并对齐Accordion标题上的内容空格(请参阅演示的第二个Accordion标题)来减小按钮大小.不过,为了方便用户,您可以让Accordion切换目标占用链接之外的所有可用空间.

请注意,所有这些都是在没有任何定制的css的情况下完成的.您将希望熟悉Bootstrap文档,这样您就知道它提供了什么,而不会重复工作.

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>


<div class="accordion" id="accordionExample">
  <div class="accordion-item">
    <div class="accordion-header d-flex align-items-center bg-info-subtle border-bottom">
      <a href="/" class="text-nowrap mx-3">Accordion Item #1</a>
      <button class="accordion-button bg-transparent shadow-none" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne"></button>
    </div>

    <div id="collapseOne" class="accordion-collapse collapse show" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the first item's accordion body.</strong> It is shown by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and
        hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>

  <div class="accordion-item">
    <div class="accordion-header d-flex justify-content-between align-items-center bg-info-subtle border-bottom">
      <a href="/" class="text-nowrap mx-3">Accordion Item #2</a>
      <div><button class="accordion-button collapsed bg-transparent shadow-none" type="button" data-bs-toggle="collapse" data-bs-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo"></button></div>
    </div>

    <div id="collapseTwo" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing
        and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>

Css相关问答推荐

当optgroup元素不包含任何未使用CSS禁用的选项元素时,如何隐藏它

如何以Angular 调整下拉控件的高度

SVG文本在移动浏览器上增长并溢出

使用高图的背景大小渐变

可以在 JavaFX CSS 中同时使用 dropshadow 和 insideshadow 吗?

如何在使用 bslib 5 的shiny 应用程序中使用样式参数排列 ui 元素

为什么左浮动上的清除:左也会影响右浮动,以及如何避免它

如何在悬停另一个部分的元素时使元素可见?

为什么 flexbox 中的 在应用居中时不会调整大小?

子组件的css会影响整个页面

具有自动调整大小的自动填充行的 CSS 网格

CSS如何防止键盘向上移动内容?

隐藏元素,但显示 CSS 生成的内容

CSS - 在 id 中 Select 类的语法

带有 flexbox 的 css 正方形网格

在 Twitter Bootstrap 中创建圆角的正确方法

位置固定宽度 100%

如何停止 setTimeout 循环?

从样式设置为 % 的元素获取宽度(以像素为单位)?

为什么 CSS 中的光标:指针效果不起作用