简体   繁体   English

带下拉菜单的切换按钮 - 引导程序

[英]Toggle Button with dropdown - bootstrap

i have a button-group with font awesome icons and a dropdown on one of them as shown in the example below.我有一个带有字体真棒图标的按钮组和一个下拉菜单,如下例所示。 I want to toggle the button status.我想切换按钮状态。 But on mouseout the .active class get removed automatically.但是在mouseout.active类会自动删除。 This seems to be associated with the focus -Event catching for all .btn -Classes in the bootstrap.js .这似乎与相关的focus -活动捕获所有.btn在-班bootstrap.js It also happens for $().button("toggle") . $().button("toggle")也会发生这种情况。

How can I work around this?我该如何解决这个问题?

A .btn-group-toggle does not work here in combination with the dropdown. .btn-group-toggle在此处与下拉列表无法结合使用。


 $('#tools button').on('click', function () { if (this.id == 'select') { // FIXME: The active class gets removed immediatly somehow $('#pencil').removeClass('active'); $('#select').addClass('active'); } else { $('#pencil').addClass('active'); $('#select').removeClass('active'); } });
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css" rel="stylesheet"/> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> <div class="btn-group-vertical d-flex" data-toggle="buttons role goup" id="tools"> <button type="button" class="btn btn-outline-secondary dropdown-toggle" id="pencil" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fas fa-pencil-alt" aria-hidden="true"></i></button> <div class="dropdown-menu" aria-labelledby="pencil" x-placement="bottom-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(0px, 38px, 0px);"> <p class="text-center">Stroke Width: <span id="pencilwidthtext">10</span></p> <input type="range" class="custom-range" id="pencilwidth" value="10" min="1"> </div> <button type="button" class="btn btn-outline-secondary" id="select" aria-pressed="false"><i class="fas fa-mouse-pointer" aria-hidden="true"></i></button> </div>

You can try with Event.stopPropagation() :您可以尝试使用Event.stopPropagation()

 $('#tools button').on('click', function (e) { if (this.id == 'select') { $('#pencil').removeClass('active'); $('#select').addClass('active'); e.stopPropagation(); } else { $('#pencil').addClass('active'); $('#select').removeClass('active'); } });
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.8.2/css/all.min.css" rel="stylesheet"/> <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js" integrity="sha384-b/U6ypiBEHpOf/4+1nzFpr53nxSS+GLCkfwBdFNTxtclqqenISfwAzpKaMNFNmj4" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> <div class="btn-group-vertical d-flex" data-toggle="buttons role goup" id="tools"> <button type="button" class="btn btn-outline-secondary dropdown-toggle" id="pencil" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fas fa-pencil-alt" aria-hidden="true"></i></button> <div class="dropdown-menu" aria-labelledby="pencil" x-placement="bottom-start" style="position: absolute; will-change: transform; top: 0px; left: 0px; transform: translate3d(0px, 38px, 0px);"> <p class="text-center">Stroke Width: <span id="pencilwidthtext">10</span></p> <input type="range" class="custom-range" id="pencilwidth" value="10" min="1"> </div> <button type="button" class="btn btn-outline-secondary" id="select" aria-pressed="false"><i class="fas fa-mouse-pointer" aria-hidden="true"></i></button> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM