显示不同的元素,如果该类具有类 active。

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

Show different element if the the class has the class active

问题

Sure, here's the translated code:

#摘要
我正在使用走马灯作为导航。

    <div class="swiper-slide state1 swiper-slide-active">状态 1</div>


    <div class="swiper-slide state2 ">状态 2</div>


    <p class="state1-content">状态 1</p>

   
    <p class="state2-content">状态 2</p>

    .state2-content {
      display:none;
    }

    <script>
      jQuery(".grad-nav .swiper-slide").addClass(function (i) {
        return 'state' + (i + 1)
     });
     if (jQuery(".state1").hasClass("swiper-slide-active")) {
       jQuery(".state1-content").fadeIn(300);
       jQuery(".state2-content").fadeOut(300);
     }

     if (jQuery(".state2").hasClass("swiper-slide-active")) {
        jQuery(".state2-content").fadeIn(300);
     jQuery(".state1-content").fadeOut(300);
     }
    </script>
#摘要
请帮助我。


我想在检测到具有活动类的情况下显示其他元素。

Note: The code has been translated, and only the translated parts are included in the response.

英文:

#Summary
I'm using a carousel as a navigation.

&lt;div class=&quot;swiper-slide state1 swiper-slide-active&quot;&gt;STATE 1&lt;/div&gt;


&lt;div class=&quot;swiper-slide state2 &quot;&gt;STATE 2&lt;/div&gt;


&lt;p class=&quot;state1-content&quot;&gt;state 1&lt;/p&gt;


&lt;p class=&quot;state2-content&quot;&gt;state 2&lt;/p&gt;

.state2-content {
  display:none;
}

&lt;script&gt;
  jQuery(&quot;.grad-nav .swiper-slide&quot;).addClass(function (i) {
    return &#39;state&#39; + (i + 1)
 });
 if (jQuery(&quot;.state1&quot;).hasClass(&quot;swiper-slide-active&quot;)) {
   jQuery(&quot;.state1-content&quot;).fadeIn(300);
   jQuery(&quot;.state2-content&quot;).fadeOut(300);
 }

 if (jQuery(&quot;.state2&quot;).hasClass(&quot;swiper-slide-active&quot;)) {
    jQuery(&quot;.state2-content&quot;).fadeIn(300);
 jQuery(&quot;.state1-content&quot;).fadeOut(300);
 }
&lt;/script&gt;

#Summary
Please help me.

I would like to show other element if the class detects that it has class active.

答案1

得分: 3

使用if else条件不是处理多个元素的通用方式。因此,在这里是适用于您情况的代码:

CSS

    <style>
        .state2-content {
             display:none;
        }
    </style>

HTML

    <div class="swiper-slide state1 swiper-slide-active">STATE 1</div>
    <div class="swiper-slide state2">STATE 2</div>
    <p class="state1-content">state 1</p>
    <p class="-content">state 2</p>

Script

    <script>
      $(document).ready(function() {
        $(".grad-nav .swiper-slide").addClass(function (index) {
          return 'state' + (index + 1);
        });
    
        $(".grad-nav .swiper-slide").on('click', function() {
          var activeState = $(this).attr('class').split(' ')[1];
          $(".grad-nav .state-content").fadeOut(300);
          $("." + activeState + "-content").fadeIn(300);
        });
      });
    </script>
英文:

Using if else condition is not a generic way for multiple elements.So, here is the code that works fine in your case:

CSS

&lt;style&gt;
    .state2-content {
         display:none;
    }
&lt;/style&gt;

HTML

&lt;div class=&quot;swiper-slide state1 swiper-slide-active&quot;&gt;STATE 1&lt;/div&gt;
&lt;div class=&quot;swiper-slide state2&quot;&gt;STATE 2&lt;/div&gt;
&lt;p class=&quot;state1-content&quot;&gt;state 1&lt;/p&gt;
&lt;p class=&quot;-content&quot;&gt;state 2&lt;/p&gt;

Script

&lt;script&gt;
  $(document).ready(function() {
    $(&quot;.grad-nav .swiper-slide&quot;).addClass(function (index) {
      return &#39;state&#39; + (index + 1);
    });

    $(&quot;.grad-nav .swiper-slide&quot;).on(&#39;click&#39;, function() {
      var activeState = $(this).attr(&#39;class&#39;).split(&#39; &#39;)[1];
      $(&quot;.grad-nav .state-content&quot;).fadeOut(300);
      $(&quot;.&quot; + activeState + &quot;-content&quot;).fadeIn(300);
    });
  });
&lt;/script&gt;

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

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

$(document).ready(function() {
    $(&quot;.grad-nav .swiper-slide&quot;).addClass(function (index) {
      return &#39;state&#39; + (index + 1);
    });

    $(&quot;.grad-nav .swiper-slide&quot;).on(&#39;click&#39;, function() {
      var activeState = $(this).attr(&#39;class&#39;).split(&#39; &#39;)[1];
      $(&quot;.grad-nav .state-content&quot;).fadeOut(300);
      $(&quot;.&quot; + activeState + &quot;-content&quot;).fadeIn(300);
    });
  });

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

.state2-content {
        display:none;
    }

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

&lt;div class=&quot;swiper-slide state1 swiper-slide-active&quot;&gt;STATE 1&lt;/div&gt;
&lt;div class=&quot;swiper-slide state2&quot;&gt;STATE 2&lt;/div&gt;
&lt;p class=&quot;state1-content&quot;&gt;state 1&lt;/p&gt;
&lt;p class=&quot;state2-content&quot;&gt;state 2&lt;/p&gt;


&lt;script src=&quot;https://code.jquery.com/jquery-3.6.0.min.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月8日 11:09:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428362.html
匿名

发表评论

匿名网友

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

确定