【问题标题】:Bootstrap outline buttons - keep background color when clickedBootstrap 轮廓按钮 - 单击时保持背景颜色
【发布时间】:2020-07-06 11:32:08
【问题描述】:

我正在使用 Bootstrap 大纲按钮。

如何在单击时保持带有背景颜色的“活动”按钮?

目前,这只发生在鼠标悬停时。

HTML

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<div class="btn-group-vertical btn-group-toggle d-flex align-items-center justify-content-center" data-toggle="buttons">
	<button type="button" class="btn btn-outline-dark btn-sm btn-block m-1 p-1" id="btn1" data-toggle="button" aria-pressed="false">
		btn1
	</button>

	<button type="button" class="btn btn-outline-dark btn-sm btn-block m-0 p-1" id="btn2" data-toggle="button" aria-pressed="false">
		btn2
	</button>

	<button type="button" class="btn btn-outline-dark btn-sm btn-block m-1 p-1" id="btn3" data-toggle="button" aria-pressed="false">
		btn3
	</button>

	<button type="button" class="btn btn-outline-dark btn-sm btn-block m-0 p-1" id="btn4" data-toggle="button" aria-pressed="false">
		btn4
	</button>
</div>

JSFidle

https://jsfiddle.net/28c0g5Lf/

【问题讨论】:

    标签: css twitter-bootstrap bootstrap-4


    【解决方案1】:

    为焦点点击按钮添加此代码

    button:focus{
      color: #fff;
        background-color: #343a40;
        border-color: #343a40;
    }
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
    <div class="btn-group-vertical btn-group-toggle d-flex align-items-center justify-content-center" data-toggle="buttons">
    	<button type="button" class="btn btn-outline-dark btn-sm btn-block m-1 p-1" id="btn1" data-toggle="button" aria-pressed="false">
    		btn1
    	</button>
    
    	<button type="button" class="btn btn-outline-dark btn-sm btn-block m-0 p-1" id="btn2" data-toggle="button" aria-pressed="false">
    		btn2
    	</button>
    
    	<button type="button" class="btn btn-outline-dark btn-sm btn-block m-1 p-1" id="btn3" data-toggle="button" aria-pressed="false">
    		btn3
    	</button>
    
    	<button type="button" class="btn btn-outline-dark btn-sm btn-block m-0 p-1" id="btn4" data-toggle="button" aria-pressed="false">
    		btn4
    	</button>
    </div>

    Working example

    【讨论】:

    • 刚刚注意到当我单击页面中的其他位置时背景颜色消失了...有什么想法吗?
    • 是的,因为按钮元素失去焦点
    • 不,我希望一次只能激活一个按钮。您的建议一开始似乎有效,但是在我单击一个按钮后(他看起来“已激活,背景颜色正常),如果我单击页面中的其他任何位置(不是另一个按钮),背景颜色就会消失......在你的工作中例如,单击按钮下方的白色区域,您就会明白我的意思。
    • @GabrielLeite 我不明白这个问题。发生这种情况是因为 onclick 其他地方按钮失去焦点属性。您必须为此使用 javascript 或 jQuery
    【解决方案2】:

    万一有人需要...

    我遵循 Tzimpo 的建议以及 this tutorial 的 JavaScript 部分并开始工作......

    HTML

    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
    <div class="btn-group-vertical btn-group-toggle d-flex align-items-center justify-content-center" id="buttonsDiv" data-toggle="buttons">
        <button type="button" class="btn btn-outline-dark btn-sm btn-block m-1 p-1" id="btn1" data-toggle="button" aria-pressed="false">
            btn1
        </button>
    
        <button type="button" class="btn btn-outline-dark btn-sm btn-block m-0 p-1" id="btn2" data-toggle="button" aria-pressed="false">
            btn2
        </button>
    
        <button type="button" class="btn btn-outline-dark btn-sm btn-block m-1 p-1" id="btn3" data-toggle="button" aria-pressed="false">
            btn3
        </button>
    
        <button type="button" class="btn btn-outline-dark btn-sm btn-block m-0 p-1" id="btn4" data-toggle="button" aria-pressed="false">
            btn4
        </button>
    </div>
    

    CSS

    button:focus{
      color: #fff;
        background-color: #343a40;
        border-color: #343a40;
    }
    

    JS

    // Get the container element
    var btnContainer = document.getElementById("buttonsDiv");
    
    // Get all buttons with class="btn" inside the container
    var btns = btnContainer.getElementsByClassName("btn");
    
    // Loop through the buttons and add the active class to the current/clicked button
    for (var i = 0; i < btns.length; i++) {
      btns[i].addEventListener("click", function() {
        var current = document.getElementsByClassName("active");
    
        // If there's no active class
        if (current.length > 0) {
          current[0].className = current[0].className.replace(" active", "");
        }
    
        // Add the active class to the current/clicked button
        this.className += " active";
      });
    }
    

    Working example

    【讨论】:

      猜你喜欢
      • 2014-10-09
      • 2021-03-28
      • 2017-08-02
      • 2021-10-18
      • 2011-09-25
      • 2017-01-26
      • 2014-10-10
      • 2013-10-24
      相关资源
      最近更新 更多