jquery change button color onclick

后端 未结 5 503
自闭症患者
自闭症患者 2021-02-04 11:04

I have multiple buttons that I use when people answer a question. How would I get the buttons to change colors when a person clicks on the button? I do not know jquery, I have b

相关标签:
5条回答
  • 2021-02-04 11:05
    $('input[type="submit"]').click(function(){
    $(this).css('color','red');
    });
    

    Use class, Demo:- http://jsfiddle.net/BX6Df/

       $('input[type="submit"]').click(function(){
              $(this).addClass('red');
    });
    

    if you want to toggle the color each click, you can try this:- http://jsfiddle.net/SMNks/

    $('input[type="submit"]').click(function(){
      $(this).toggleClass('red');
    });
    
    
    .red
    {
        background-color:red;
    }
    

    Updated answer for your comment.

    http://jsfiddle.net/H2Xhw/

    $('input[type="submit"]').click(function(){
        $('input[type="submit"].red').removeClass('red')
            $(this).addClass('red');
    });
    
    0 讨论(0)
  • 2021-02-04 11:10

    I would just create a separate CSS class:

    .ButtonClicked {
        background-color:red;
    }
    

    And then add the class on click:

    $('#ButtonId').on('click',function(){
        !$(this).hasClass('ButtonClicked') ? addClass('ButtonClicked') : '';
    });
    

    This should do what you're looking for, showing by this jsFiddle. If you're curious about the logic with the ? and such, its called ternary (or conditional) operators, and its just a concise way to do the simple if logic to check if the class has already been added.

    You can also create the ability to have an "on/off" switch feel by toggling the class:

    $('#ButtonId').on('click',function(){
        $(this).toggleClass('ButtonClicked');
    });
    

    Shown by this jsFiddle. Just food for thought.

    0 讨论(0)
  • 2021-02-04 11:10

    Use css:

    <style>
    input[name=btnsubmit]:active {
    color: green;
    }
    </style>
    
    0 讨论(0)
  • 2021-02-04 11:25

    You have to include the jquery framework in your document head from a cdn for example:

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
    

    Then you have to include a own script for example:

    (function( $ ) {
    
      $(document).ready(function(){
          $('input').click(function() {
              $(this).css('background-color', 'green');
          }
      });
    
    
      $(window).load(function() { 
      });
    
    })( jQuery );
    

    This part is a mapping of the $ to jQuery, so actually it is jQuery('selector').function();

    (function( $ ) {
    
    })( jQuery );
    

    Here you can find die api of jquery where all functions are listed with examples and explanation: http://api.jquery.com/

    0 讨论(0)
  • 2021-02-04 11:32

    Add this code to your page:

    <script type="text/javascript">
    $(document).ready(function() {
       $("input[type='submit']").click(function(){
          $(this).css('background-color','red');
        });
    });
    </script>
    
    0 讨论(0)
提交回复
热议问题