简体   繁体   English

基于引导折叠更改按钮 state

[英]changing button based on bootstrap collapse state

I am using bootstrap.我正在使用引导程序。

I have the HTML code below:我有下面的 HTML 代码:

 <div class="row">
        <button id="dog-btn" data-toggle="collapse" data-target="#dog-section" onclick="petToggle()">Show Pet</button>
    </div>


    <div class="col-12 collapse" id="dog-section">

I have the JS below:我有下面的JS:

function petToggle() {
          var dogDiv = document.getElementById("dog-section");
          var dogBtn = document.getElementById("dog-btn");
 
        }

In my website the starting state of dog-section is variable.在我的网站中,dog-section 的起始 state 是可变的。 Sometimes it could be visible sometimes it can be closed.有时它可能是可见的,有时它可以关闭。 I tried using:hidden and:visible with jquery to get the initial state of the div but it didn't work as it always returned true or always returned false.我尝试使用:hidden 和:visible 和 jquery 来获取 div 的初始 state 但它不起作用,因为它总是返回 true 或总是返回 false。 My desired outcome: if dog-section is hidden because of Bootstrap dropdown I want the button to read Show Pet.我想要的结果:如果由于 Bootstrap 下拉菜单而隐藏了狗部分,我希望按钮读取显示宠物。 If the dog-section is visible, I want it to read Hide Pet.如果狗部分可见,我希望它阅读隐藏宠物。

Can anyone help?任何人都可以帮忙吗?

Use a conditional to check if the css is set to visible if it is then set it to hidden .使用条件检查 css 是否设置为visible如果然后将其设置为hidden

With jQuery:使用 jQuery:

To check:去检查:

if(dogSection.css('visibility') === 'hidden'){
  // make it visible
}else{
  // hide it
}

To set visible:设置可见:

dogSection.css('visibility', 'visible')

To set hidden:设置隐藏:

dogSection.css('visibility', 'hidden')

 const $btn = $('#dog-btn') const $dogSection = $('#dog-section') function petToggle(){ $btn.click(function(){ if($dogSection.css('visibility') === 'hidden'){ $dogSection.css('visibility', 'visible') $btn.text("Hide Pet Section") }else{ $dogSection.css('visibility', 'hidden') $btn.text("Show Pet Section") } }) } petToggle()
 #dog-section { visibility: hidden; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="row"> <button id="dog-btn" data-toggle="collapse" data-target="#dog-section">Show Pet Section</button> </div> <div class="col-12 collapse" id="dog-section"> <img src="https://www.petmd.com/sites/default/files/sleeping-dogs-curled-up.jpg" alt="Sleeping Dog" width="450" height="355"> <img href=""> </div>

Using vanilla Javascript...使用香草 Javascript...

Depending on how you are displaying your #dog-section in the DOM you can use one of the following.根据您在 DOM 中显示#dog-section的方式,您可以使用以下选项之一。

if(getComputedStyle(dogSection).getPropertyValue('visibility') === 'hidden')
if(getComputedStyle(dogSection).getPropertyValue('display') === 'none')
if(dogSection.style.visibility === 'hidden';
if(dogSection.style.display === 'none';

Then set it using one of the following:然后使用以下方法之一进行设置:

dogSection.style.setProperty('visibility', 'visible')
dogSection.style.visibility = 'visible'

 const btn = document.getElementById('dog-btn') const dogSection = document.getElementById('dog-section') function petToggle(){ btn.addEventListener('click', function(){ if(getComputedStyle(dogSection).getPropertyValue('visibility') === 'hidden'){ dogSection.style.setProperty('visibility', 'visible') btn.textContent = "Hide Pet Section" }else{ dogSection.style.setProperty('visibility', 'hidden') btn.textContent = "Show Pet Section" } }) } petToggle()
 #dog-section { visibility: hidden; }
 <div class="row"> <button id="dog-btn" data-toggle="collapse" data-target="#dog-section">Show Pet Section</button> </div> <div class="col-12 collapse" id="dog-section"> <img src="https://www.petmd.com/sites/default/files/sleeping-dogs-curled-up.jpg" alt="Sleeping Dog" width="450" height="355"> <img href=""> </div>

Remove the petToggle function.移除petToggle function。 You need to use the collapse events to achieve what you want.您需要使用折叠事件来实现您想要的。 Here's documentation .这是 文档

Since dog-section might be hidden or shown initially, you can check the display style of the section and change button text accordingly.由于 dog-section 最初可能被隐藏或显示,您可以检查该部分的显示样式并相应地更改按钮文本。

 // check the initial display state of the dog-section // if it's hidden, set the button text to "Show Pet" // if it's shown, set the button text to "Hide Pet" if ($("#dog-section").css("display") === "none") { $("#dog-btn").text("Show Pet"); } else { $("#dog-btn").text("Hide Pet"); } // set the button text to "Hide Pet" when dog-section is shown $("#dog-section").on("show.bs.collapse", function () { $("#dog-btn").text("Hide Pet"); }); // set the button text to "Show Pet" when dog-section is hidden $("#dog-section").on("hide.bs.collapse", function () { $("#dog-btn").text("Show Pet"); });
 <link href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/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://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.bundle.min.js"></script> <div class="container"> <div class="row"> <button id="dog-btn" data-toggle="collapse" data-target="#dog-section">Show Pet</button> </div> <div class="row"> <div class="col-12 collapse" id="dog-section"> Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsam quia suscipit amet ex aut. Odit error dolorem tempore, officiis sed, fugit in voluptate laboriosam quas illo ipsa alias magnam. Odit. </div> </div> </div>

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

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