“Comment changer le style CSS en clic” Réponses codées

Comment changer le style CSS en clic

const elem = document.getElementById('elem-id');
// Now that we got the element we needed,
// there are 3 ways that we can style it when it's clicked

// method 1: .onclick
elem.onclick = () => {
  elem.style.backgroundColor = 'red';
}

// method 2: .addEventListener
elem.addEventListener('click', function(){
  elem.style.backgroundColor = 'red'; 
})

// method 3: function
function funcName(){
  elem.style.backgroundColor = 'red';
}
// now that we declared the function we need to call it on click,
// we can use both method 1 & 2, replace the code and call the function there
// or we can call it on the html attribute 'onclick'

// <div id="elem-id" onclick="funcName"></div>
ST111

JS onclick Change CSS

<div id="foo">hello world!</div>
<img src="zoom.png" id="image" />
<script>
  $('#image').click(function() {
    $('#foo').css({
        'background-color': 'red',
        'color': 'white',
        'font-size': '44px'
    });
});
  </script>
Undefined

Comment garder le style après le clic

#activate-div{display:none}    

.my-div{background-color:#FFF} 

#activate-div:checked ~ label 
.my-div{background-color:#000}
Better Butterfly

Réponses similaires à “Comment changer le style CSS en clic”

Questions similaires à “Comment changer le style CSS en clic”

Plus de réponses similaires à “Comment changer le style CSS en clic” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code