[英]Javascript to change button text back and forth using bootstrap 4 & fontawesome icons
I'm using bootstrap 4 to create a button with a dropdown menu.我正在使用 bootstrap 4 创建一个带有下拉菜单的按钮。 The functionality I need is that the button when closed contains the word open and a fontawesome icon beside it.
我需要的功能是按钮关闭时包含打开一词和旁边的字体图标。 When opened it should say close with an icon beside it.
打开时,它应该说关闭,旁边有一个图标。
I'm currently trying this我目前正在尝试这个
function toggle() { const btn = document.body.querySelector("#dropdownMenu2"); if (btn.innerText == "Open") { btn.innerHTML = "<i class='fa fa-plus-circle' aria-hidden='true'></i>" + "Open"; } else { btn.innerHTML = "<i class='fa fa-minus-circle' aria-hidden='true'></i>" + "Close"; } }
.dropdown-toggle { border-radius: 0; border-style: solid; border-color: #9d2235; background-color: #9d2235; color: #fff; padding: 8px 20px; }
<button class="dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" onclick="toggle()"> <i class="fa fa-plus-circle" aria-hidden="true"></i> Actions </button>
The button changes to close on the first click but never changes back to open when clicked again.该按钮在第一次单击时更改为关闭,但在再次单击时永远不会更改回打开。
instead of trying to alter the dom
.而不是试图改变
dom
。 Why dont you just add a second button with exact details and then just add same class on both the buttons and use jquery(since bootstrap 4 uses jquery) to hide and display other like following为什么不添加第二个具有确切详细信息的按钮,然后在两个按钮上添加相同的 class 并使用 jquery(因为 bootstrap 4 使用 jquery)隐藏和显示其他按钮,如下所示
$('.buttonClass').toggle();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.