Ajouter un style javascript
// Bad:
element.setAttribute("style", "background-color: red;");
// Good:
element.style.backgroundColor = "red";
Dizzy Dog
// Bad:
element.setAttribute("style", "background-color: red;");
// Good:
element.style.backgroundColor = "red";
var a = document.getElementById('myElementID');
a.href = "http://www.mywebsite.com";
var elem = document.querySelector('#some-element');
// Set color to purple
elem.style.color = 'purple';
// Set the background color to a light gray
elem.style.backgroundColor = '#e5e5e5';
// Set the height to 150px
elem.style.height = '150px';
// Getting the element first
var myElement = document.querySelector("#myElement");
// examples for updating styles
myElement.style.color = "blue";
myElement.style.backgroundColor = "red";
// Add style rule to an element without altering other styles.
element.style.backgroundColor = 'red'