Javascript/Jquery to change class onclick?

后端 未结 8 2078
北海茫月
北海茫月 2020-12-01 15:13

I want to change a class onclick. What I have at the moment:



        
相关标签:
8条回答
  • 2020-12-01 15:21
          <div class="liveChatContainer online">
            <div class="actions" id="change">
             <span class="item title">Need help?</span>
                  <a href="/test" onclick="demo()"><i class="fa fa-smile-o"></i>Chat</a>
                  <a href="/test"><i class="fa fa-smile-o"></i>Call</a>
                  <a href="/test"><i class="fa fa-smile-o"></i>Email</a>
            </div>
                  <a href="#" class="liveChatLabel">Contact</a>
         </div>
                 <style>
                   .actions_one{
                   background-color: red;
                          } 
                  </style>
    
    
    
               <script>
              function demo(){
          document.getElementById("change").setAttribute("class","actions_one");}
                </script>      
    
    0 讨论(0)
  • 2020-12-01 15:23

    With jquery you could do to sth. like this, which will simply switch classes.

    $('.showhide').click(function() {
        $(this).removeClass('myclass');
        $(this).addClass('showhidenew');
    });
    

    If you want to switch classes back and forth on each click, you can use toggleClass, like so:

    $('.showhide').click(function() {
        $(this).toggleClass('myclass');
        $(this).toggleClass('showhidenew');
    });
    
    0 讨论(0)
  • 2020-12-01 15:26

    Your getElementById is looking for an element with id "myclass", but in your html the id of the DIV is showhide. Change to:

    <script>
    function changeclass() {
    
    var NAME = document.getElementById("showhide")
    
    NAME.className="mynewclass"
    
    } 
    </script>
    

    Unless you are trying to target a different element with the id "myclass", then you need to make sure such an element exists.

    0 讨论(0)
  • 2020-12-01 15:27

    For a super succinct with jQuery approach try:

    <div onclick="$(this).toggleClass('newclass')">click me</div>
    

    Or pure JS:

    <div onclick="this.classList.toggle('newclass');">click me</div>
    
    0 讨论(0)
  • 2020-12-01 15:30

    I think you mean that you want want an onclick event that changes a class.

    Here is the answer if someone visits this question and is literally looking to assign a class and it's onclick with JQUERY.

    It is somewhat counter-intuitive, but if you want to change the onclick event by changing the class you need to declare the onclick event to apply to elements of a parent object.

    HTML

    <div id="containerid">
         Text <a class="myClass" href="#" />info</a>
         Other Text <div class="myClass">other info</div>
    </div>
    <div id="showhide" class="meta-info">hide info</div>
    

    Document Ready

    $(function() {
         $("#containerid").on("click",".myclass",function(e){ /*do stuff*/ }
         $("#containerid").on("click",".mynewclass",function(e){ /*do different stuff*/ }
         $("#showhide").click(function() {changeclass()}
    });
    

    Slight Tweak to Your Javascript

    <script>
    function changeclass() {
            $(".myClass,.myNewClass").toggleClass('myNewClass').toggleClass('myClass');
    } 
    </script>
    

    If you can't reliably identify a parent object you can do something like this.

    $(function() {
         $(document).on("click",".myclass",function(e){}
         $(document).on("click",".mynewclass",function(e){}
    });
    

    If you just want to hide the items you might find it simpler to use .hide() and .show().

    0 讨论(0)
  • 2020-12-01 15:33

    Just using this will add "mynewclass" to the element with the id myElement and revert it on the next call.

    <div id="showhide" class="meta-info" onclick="changeclass(this);">
    
    function changeclass(element) {
        $(element).toggleClass('mynewclass');
    }
    

    Or for a slighly more jQuery way (you would run this after the DOM is loaded)

    <div id="showhide" class="meta-info">
    
    $('#showhide').click(function() {
        $(this).toggleClass('mynewclass');
    });
    

    See a working example of this here: http://jsfiddle.net/S76WN/

    0 讨论(0)
提交回复
热议问题