Change CSS properties on click

后端 未结 8 2331
情话喂你
情话喂你 2020-11-29 22:53

I am trying to change the CSS of one element on click of another element. I\'ve searched a lot but nothing works perfectly. Currently I am using the below code, but it doesn

8条回答
  •  攒了一身酷
    2020-11-29 23:46

    Firstly, using on* attributes to add event handlers is a very outdated way of achieving what you want. As you've tagged your question with jQuery, here's a jQuery implementation:

    hello world!
    $('#image').click(function() {
        $('#foo').css({
            'background-color': 'red',
            'color': 'white',
            'font-size': '44px'
        });
    });
    

    A more efficient method is to put those styles into a class, and then add that class onclick, like this:

    $('#image').click(function() {
      $('#foo').addClass('myClass');
    });
    .myClass {
      background-color: red;
      color: white;
      font-size: 44px;
    }
    
    
    hello world!

提交回复
热议问题