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

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

Show different element if the the class has the class active

问题

Sure, here's the translated code:

  1. #摘要
  2. 我正在使用走马灯作为导航。
  3. <div class="swiper-slide state1 swiper-slide-active">状态 1</div>
  4. <div class="swiper-slide state2 ">状态 2</div>
  5. <p class="state1-content">状态 1</p>
  6. <p class="state2-content">状态 2</p>
  7. .state2-content {
  8. display:none;
  9. }
  10. <script>
  11. jQuery(".grad-nav .swiper-slide").addClass(function (i) {
  12. return 'state' + (i + 1)
  13. });
  14. if (jQuery(".state1").hasClass("swiper-slide-active")) {
  15. jQuery(".state1-content").fadeIn(300);
  16. jQuery(".state2-content").fadeOut(300);
  17. }
  18. if (jQuery(".state2").hasClass("swiper-slide-active")) {
  19. jQuery(".state2-content").fadeIn(300);
  20. jQuery(".state1-content").fadeOut(300);
  21. }
  22. </script>
  23. #摘要
  24. 请帮助我。
  25. 我想在检测到具有活动类的情况下显示其他元素。

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.

  1. &lt;div class=&quot;swiper-slide state1 swiper-slide-active&quot;&gt;STATE 1&lt;/div&gt;
  2. &lt;div class=&quot;swiper-slide state2 &quot;&gt;STATE 2&lt;/div&gt;
  3. &lt;p class=&quot;state1-content&quot;&gt;state 1&lt;/p&gt;
  4. &lt;p class=&quot;state2-content&quot;&gt;state 2&lt;/p&gt;
  5. .state2-content {
  6. display:none;
  7. }
  8. &lt;script&gt;
  9. jQuery(&quot;.grad-nav .swiper-slide&quot;).addClass(function (i) {
  10. return &#39;state&#39; + (i + 1)
  11. });
  12. if (jQuery(&quot;.state1&quot;).hasClass(&quot;swiper-slide-active&quot;)) {
  13. jQuery(&quot;.state1-content&quot;).fadeIn(300);
  14. jQuery(&quot;.state2-content&quot;).fadeOut(300);
  15. }
  16. if (jQuery(&quot;.state2&quot;).hasClass(&quot;swiper-slide-active&quot;)) {
  17. jQuery(&quot;.state2-content&quot;).fadeIn(300);
  18. jQuery(&quot;.state1-content&quot;).fadeOut(300);
  19. }
  20. &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

  1. <style>
  2. .state2-content {
  3. display:none;
  4. }
  5. </style>

HTML

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

Script

  1. <script>
  2. $(document).ready(function() {
  3. $(".grad-nav .swiper-slide").addClass(function (index) {
  4. return 'state' + (index + 1);
  5. });
  6. $(".grad-nav .swiper-slide").on('click', function() {
  7. var activeState = $(this).attr('class').split(' ')[1];
  8. $(".grad-nav .state-content").fadeOut(300);
  9. $("." + activeState + "-content").fadeIn(300);
  10. });
  11. });
  12. </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

  1. &lt;style&gt;
  2. .state2-content {
  3. display:none;
  4. }
  5. &lt;/style&gt;

HTML

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

Script

  1. &lt;script&gt;
  2. $(document).ready(function() {
  3. $(&quot;.grad-nav .swiper-slide&quot;).addClass(function (index) {
  4. return &#39;state&#39; + (index + 1);
  5. });
  6. $(&quot;.grad-nav .swiper-slide&quot;).on(&#39;click&#39;, function() {
  7. var activeState = $(this).attr(&#39;class&#39;).split(&#39; &#39;)[1];
  8. $(&quot;.grad-nav .state-content&quot;).fadeOut(300);
  9. $(&quot;.&quot; + activeState + &quot;-content&quot;).fadeIn(300);
  10. });
  11. });
  12. &lt;/script&gt;

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

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

  1. $(document).ready(function() {
  2. $(&quot;.grad-nav .swiper-slide&quot;).addClass(function (index) {
  3. return &#39;state&#39; + (index + 1);
  4. });
  5. $(&quot;.grad-nav .swiper-slide&quot;).on(&#39;click&#39;, function() {
  6. var activeState = $(this).attr(&#39;class&#39;).split(&#39; &#39;)[1];
  7. $(&quot;.grad-nav .state-content&quot;).fadeOut(300);
  8. $(&quot;.&quot; + activeState + &quot;-content&quot;).fadeIn(300);
  9. });
  10. });

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

  1. .state2-content {
  2. display:none;
  3. }

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

  1. &lt;div class=&quot;swiper-slide state1 swiper-slide-active&quot;&gt;STATE 1&lt;/div&gt;
  2. &lt;div class=&quot;swiper-slide state2&quot;&gt;STATE 2&lt;/div&gt;
  3. &lt;p class=&quot;state1-content&quot;&gt;state 1&lt;/p&gt;
  4. &lt;p class=&quot;state2-content&quot;&gt;state 2&lt;/p&gt;
  5. &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:

确定