如何使用jQuery切换多个div

huangapple go评论67阅读模式
英文:

How to toggle multiple divs using jQuery

问题

我有几个div,应该在相应的按钮被点击时显示/隐藏。在初始状态下,所有按钮和所有div都是可见的。当点击第一个按钮时,只有相应的div应该可见。当点击第二个按钮时,相应的div也应该可见,依此类推。点击按钮应该切换按钮和相应div的状态。

我不确定是否可以在不使用cookie或本地存储令牌的情况下实现这一点。到目前为止,我创建了以下代码,只允许我在第一次点击时停用div

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div>
  <button class="button" target="1">Button 1</button>
  <button class="button" target="2">Button 2</button>
  <button class="button" target="3">Button 3</button>
</div>

<div id="mydiv1">
  <p>Div 1</p>
</div>

<div id="mydiv2">
  <p>Div 2</p>
</div>

<div id="mydiv3">
  <p>Div 3</p>
</div>
$(function() {
    $('.button').click(function(){
        $(this).toggleClass('inactive');
        $('#mydiv'+$(this).attr('target')).toggle();
    });
});

这段代码通过使用jQuery实现了按钮点击后切换div的可见性。

英文:

I have several divs which should be shown / hidden when the corresponding button is clicked. In the initial state all buttons and all divs are visible. When the first button is clicked, only the corresponding div should be visible. When the second button is clicked, the corresponding div should be visible as well etc. The click on a button should toggle the state of the button and the corresponding div.

I'm not sure if this can be realized without placing a cookie or local storage token. So far I've created the following code, which only allows me to deactivate the divs with one first click.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(function() {
    $(&#39;.button&#39;).click(function(){
        $(this).toggleClass(&#39;inactive&#39;);
        $(&#39;#mydiv&#39;+$(this).attr(&#39;target&#39;)).toggle();
    });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;div&gt;
  &lt;button class=&quot;button&quot; target=&quot;1&quot;&gt;Button 1&lt;/button&gt;
  &lt;button class=&quot;button&quot; target=&quot;2&quot;&gt;Button 2&lt;/button&gt;
  &lt;button class=&quot;button&quot; target=&quot;3&quot;&gt;Button 3&lt;/button&gt;
&lt;/div&gt;

&lt;div id=&quot;mydiv1&quot;&gt;
  &lt;p&gt;Div 1&lt;/p&gt;
&lt;/div&gt;

&lt;div id=&quot;mydiv2&quot;&gt;
  &lt;p&gt;Div 2&lt;/p&gt;
&lt;/div&gt;

&lt;div id=&quot;mydiv3&quot;&gt;
  &lt;p&gt;Div 3&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 3

如果您想要仅切换您刚刚点击的按钮/ div,首先您必须重置所有元素的先前状态为初始状态(删除类inactive并显示所有div),然后才能切换状态。

$(function() {
    $('input[type="checkbox"]').change(function() {
        let checked = $('input[type="checkbox"]:checked');
        $('.inactive').removeClass('inactive');

        if (checked.length == 0) {
            $('.subdiv').show();
            return;
        }

        $('input[type="checkbox"]:not(:checked)').closest('label').addClass('inactive');

        $('.subdiv').hide();

        checked.each(function() {
            $('#mydiv' + $(this).val()).toggle();
        });
    });
});
.inactive {
    color: red;
}

label input {
    display: none;
}

label {
    border: 1px solid gray;
    border-radius: 3px;
    padding: 2px 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div>
    <label><input type="checkbox" value="1"/>Button 1</label>
    <label><input type="checkbox" value="2"/>Button 2</label>
    <label><input type="checkbox" value="3"/>Button 3</label>
</div>

<div id="mydiv1" class="subdiv">
    <p>Div 1</p>
</div>

<div id="mydiv2" class="subdiv">
    <p>Div 2</p>
</div>

<div id="mydiv3" class="subdiv">
    <p>Div 3</p>
</div>
英文:

If you want to toggle only the button/div that you just clicked, first you have to reset previous state of all elements to initial (remove class inactive and show all divs) and only than toggle state.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(function() {
    $(&#39;input[type=&quot;checkbox&quot;]&#39;).change(function() {
      let checked = $(&#39;input[type=&quot;checkbox&quot;]:checked&#39;);
      $(&#39;.inactive&#39;).removeClass(&#39;inactive&#39;);
      
      if (checked.length == 0) {
        $(&#39;.subdiv&#39;).show();
        
        return;
      }
      
      $(&#39;input[type=&quot;checkbox&quot;]:not(:checked)&#39;).closest(&#39;label&#39;).addClass(&#39;inactive&#39;);

      $(&#39;.subdiv&#39;).hide();
      
      checked.each(function () {
        $(&#39;#mydiv&#39; + $(this).val()).toggle();
      });
    });
});

<!-- language: lang-css -->

.inactive {
  color: red;
}

label input {
  display: none;
}

label {
  border: 1px solid gray;
  border-radius: 3px;
  padding: 2px 5px;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;div&gt;
  &lt;label&gt;&lt;input type=&quot;checkbox&quot; value=&quot;1&quot;/&gt;Button 1&lt;/label&gt;
  &lt;label&gt;&lt;input type=&quot;checkbox&quot; value=&quot;2&quot;/&gt;Button 2&lt;/label&gt;
  &lt;label&gt;&lt;input type=&quot;checkbox&quot; value=&quot;3&quot;/&gt;Button 3&lt;/label&gt;
&lt;/div&gt;

&lt;div id=&quot;mydiv1&quot; class=&quot;subdiv&quot;&gt;
  &lt;p&gt;Div 1&lt;/p&gt;
&lt;/div&gt;

&lt;div id=&quot;mydiv2&quot; class=&quot;subdiv&quot;&gt;
  &lt;p&gt;Div 2&lt;/p&gt;
&lt;/div&gt;

&lt;div id=&quot;mydiv3&quot; class=&quot;subdiv&quot;&gt;
  &lt;p&gt;Div 3&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 2

你需要一开始隐藏div,并在点击正确的按钮时显示正确的div

$(function() {
  $('.button').click(function() {
    $(this).toggleClass('active');
    $('#mydiv' + $(this).attr('target')).toggle();
  });
});
/*
  隐藏所有带有以 "mydiv" 开头的 id 的 div
*/
div[id^="mydiv"] {
  display: none;
}

.active {
  background-color: green;
  color: white;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div>
  <button class="button" target="1">Button 1</button>
  <button class="button" target="2">Button 2</button>
  <button class="button" target="3">Button 3</button>
</div>

<div id="mydiv1">
  <p>Div 1</p>
</div>

<div id="mydiv2">
  <p>Div 2</p>
</div>

<div id="mydiv3">
  <p>Div 3</p>
</div>
英文:

You'd have to initially hide the divs and show the correct one when the right button is clicked.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(function() {
  $(&#39;.button&#39;).click(function() {
    $(this).toggleClass(&#39;active&#39;);
    $(&#39;#mydiv&#39; + $(this).attr(&#39;target&#39;)).toggle();
  });
});

<!-- language: lang-css -->

/*
  Hide all divs that have an id starting with &quot;mydiv&quot;
*/

div[id^=&quot;mydiv&quot;] {
  display: none;
}

.active {
  background-color: green;
  color: white;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;div&gt;
  &lt;button class=&quot;button&quot; target=&quot;1&quot;&gt;Button 1&lt;/button&gt;
  &lt;button class=&quot;button&quot; target=&quot;2&quot;&gt;Button 2&lt;/button&gt;
  &lt;button class=&quot;button&quot; target=&quot;3&quot;&gt;Button 3&lt;/button&gt;
&lt;/div&gt;

&lt;div id=&quot;mydiv1&quot;&gt;
  &lt;p&gt;Div 1&lt;/p&gt;
&lt;/div&gt;

&lt;div id=&quot;mydiv2&quot;&gt;
  &lt;p&gt;Div 2&lt;/p&gt;
&lt;/div&gt;

&lt;div id=&quot;mydiv3&quot;&gt;
  &lt;p&gt;Div 3&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

答案3

得分: 0

你是否需要使用一个切换按钮,或者也可以通过其他方式解决它?

英文:

Have you to use a toggle or you can also solve it with a walk around?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(function() {
  $(&#39;.button&#39;).click(function() {
  $(&#39;.inactive&#39;).removeClass(&#39;inactive&#39;);
    $(this).toggleClass(&#39;active&#39;);
    $(&#39;.subdiv&#39;).show();
 
    $(&#39;.mydiv&#39; + $(this).attr(&#39;target&#39;)).toggle();
  });
});

<!-- language: lang-css -->

/*
  Hide all divs that have an id starting with &quot;mydiv&quot;
*/

.active {
  background-color: green;
  color: white;
}

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;

&lt;div&gt;
  &lt;button class=&quot;button&quot; target=&quot;1&quot;&gt;Button 1&lt;/button&gt;
  &lt;button class=&quot;button&quot; target=&quot;2&quot;&gt;Button 2&lt;/button&gt;
  &lt;button class=&quot;button&quot; target=&quot;3&quot;&gt;Button 3&lt;/button&gt;
&lt;/div&gt;

&lt;div class=&quot;mydiv1&quot; hidden&gt;
  &lt;p&gt;Div 1&lt;/p&gt;
&lt;/div&gt;

&lt;div class=&quot;mydiv2&quot; hidden&gt;
  &lt;p&gt;Div 2&lt;/p&gt;
&lt;/div&gt;

&lt;div class=&quot;mydiv3&quot; hidden&gt;
  &lt;p&gt;Div 3&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月6日 15:49:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定