jQuery切换隐藏/显示div;如何防止再次点击同一个锚时隐藏div

kh212irz  于 2024-01-07  发布在  jQuery
关注(0)|答案(1)|浏览(217)

我有2个div,当点击锚时会切换。点击锚1;显示div 1,div 2隐藏。当第二次点击锚1时; div 1也隐藏。这不应该!我如何防止这种情况?
我尝试了if( !$(this).closest('li').hasClass('.active') ) {,但这不起作用

  1. $('.showSingle').click(function() {
  2. if (!$(this).closest('li').hasClass('.active')) {
  3. $('.active').closest('li').removeClass('active');
  4. $(this).closest('li').addClass('active');
  5. $('.targetDiv').not('#div' + $(this).attr('target')).hide();
  6. $('#div' + $(this).attr('target')).toggle();
  7. }
  8. });

个字符
Fiddle

ccgok5k5

ccgok5k51#

差不多了
这更简单,也更有效

  1. $(function() {
  2. $('.showSingle').on('click', function() {
  3. const target = $(this).attr('target');
  4. const $li = $(this).closest('li');
  5. $('li.active').removeClass('active'); // remove all active
  6. $li.addClass('active');
  7. $('.targetDiv').hide(); // hide all
  8. $(`#div${target}`).show();
  9. });
  10. });

个字符
然而,正如评论所说,不要使用target,而应使用data-attributes,或者在我看来更好,href

  1. $(function() {
  2. $nav = $('#nav').on('click', '.showSingle', function() {
  3. const target = $(this).attr('href');
  4. const $li = $(this).closest('li');
  5. $('li.active', $nav).removeClass('active'); // remove all active
  6. $li.addClass('active');
  7. $('.targetDiv').hide(); // hide all
  8. console.log(target)
  9. $(target).show();
  10. });
  11. });

x

  1. a {
  2. text-decoration: none;
  3. }
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  2. <ul id="nav">
  3. <li class="active">
  4. <a class="showSingle" href="#div1">Show div 1</a>
  5. </li>
  6. <li>
  7. <a class="showSingle" href="#div2">Show div 2</a>
  8. </li>
  9. </ul>
  10. <br /><br />
  11. <section id="div1" class="targetDiv">
  12. Content div 1
  13. </section>
  14. <section id="div2" class="targetDiv" hidden>
  15. Content div 2
  16. </section>

的一种或多种
下面是一个CSS改进,因为如果从外部链接,它将显示目标div

  1. $(function() {
  2. const $nav = $('#nav').on('click', '.showSingle', function() {
  3. const target = $(this).attr('href');
  4. const $li = $(this).closest('li');
  5. $('li.active', $nav).removeClass('active'); // remove all active
  6. $li.addClass('active');
  7. });
  8. const hash = location.hash || $('.showSingle').first().attr('href');
  9. if (hash && hash.startsWith('#div')) {
  10. $activate = $nav.find(`[href="${hash}"]`);
  11. if ($activate.length ===1) $activate.closest('li').addClass('active');
  12. }
  13. });

x

  1. a {
  2. text-decoration: none;
  3. }
  4. .active { background-color: green }
  5. .targetDiv {
  6. display: none; /* Hide all targetDiv elements by default */
  7. }
  8. .targetDiv:target {
  9. display: block; /* Show the targetDiv if it's the target of the URL fragment */
  10. }
  1. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  2. <ul id="nav">
  3. <li>
  4. <a class="showSingle" href="#div1">Show div 1</a>
  5. </li>
  6. <li>
  7. <a class="showSingle" href="#div2">Show div 2</a>
  8. </li>
  9. </ul>
  10. <br /><br />
  11. <section id="div1" class="targetDiv">
  12. Content div 1
  13. </section>
  14. <section id="div2" class="targetDiv">
  15. Content div 2
  16. </section>
展开查看全部

相关问题