簡體   English   中英

如何在div顯示時添加類(活動),在隱藏時如何添加類(活動)

[英]How to add class(active) when div show, when it's hide the remove class(active)

請告訴我單擊時如何添加活動類,不單擊時我如何添加活動類。

請檢查我的代碼並告訴我該怎么做,謝謝

我這樣做了,但是什么也沒做。

   jQuery(function(){
     jQuery('#showall').click(function(){
           jQuery('#faq1').show();
    });
    jQuery('.showSingle').click(function(){
          jQuery('.targetDiv').hide().removeclass('active');


          jQuery('#faq'+jQuery(this).attr('target')).show().addClass( 'active' );

  });

 $("p").click(function(e){ $("p").removeClass("active"); $(this).addClass("active"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js"></script> <p>Link 1</p> <p>Link 2</p> 

不要使用show() hide()方法。

添加到您的CSS:

.yourTargetElementsSelector { display: none; }
.yourTargetElementsSelector.active { display: block; }

因此,當您添加.active類時,將顯示該元素,否則會將其隱藏。

HTML和腳本

<img src="path/img.png" id="IdForImage"/>

var addActive = document.getElementById("IdForImage"); addActive.classList.add('active'); addActive.classList.remove('active');

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM