'How to change the background colors of all <em> elements
I am trying to change the background colors of all italized text, instead of using a span on every single word through the paragraph.
It says <em> next to the italized text.
I have tried
$(".em").css({
"background-color":"#d9f9f9",
});
or/and tried this:
var elem=document.getElementByTagName(em)[1];
elem.style.backgroundColor='#d9f9f9';
Solution 1:[1]
See querySelectorAll:
Notices this method return an array, so we should loop it:
var emList = document.querySelectorAll('em');
[].forEach.call(emList , function(em) {
// do whatever
em.style.color = "red";
});
Solution 2:[2]
Your first suggestion says ".em" where it should say "em". Your seconds suggestion says em where it should say "em".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | navylover |
| Solution 2 | Mark Roberts |
