I am trying to create a script that gives each individual letter inside "p" tags on my website a random color. The coloring part works fine, the problem is that my script overwrites "br" and "a" tags. Is there any workaround for this?
Here is my code so far:
function changeColor() {
var paragraphs = document.getElementsByTagName("p");
for(var i = 0; i < paragraphs.length; i++)
{
var innerText = paragraphs[i].innerText;
var innerTextSplit = innerText.split("");
paragraphs[i].innerText = ""
for(var j = 0; j < innerTextSplit.length; j++) {
var randomColor = "rgb(" + Math.floor((Math.random() * 255) + 1) + ", " + Math.floor((Math.random() * 255) + 1) + ", " + Math.floor((Math.random() * 255) + 1) + ");"
innerTextSplit[j] = '<span style="color: ' + randomColor + '">' + innerTextSplit[j] + '</span>';
paragraphs[i].innerHTML += innerTextSplit[j];
}
}
}
Any help is greatly appreciated, thanks in advance :)
-jAndersen
Aucun commentaire:
Enregistrer un commentaire