0
I need to change the color of background
, the colour of the texts and links of all the Divs of one website.
I was able to do this in Javascript with the following code:
<script type="text/javascript">
function altoContraste() {
var divs = document.getElementsByTagName("div");
var links = geral.getElementsByTagName("a");
for (var i = 0; i < divs.length; i++) {
divs[i].style.background= '#000';
divs[i].style.color= '#FFF';
}
for (var i = 0; i < links.length; i++) {
links[i].style.color = '#FF0';
}
}
</script>
But I need to do it using jQuery, I don’t understand jQuery, but I researched some places how to do it and I arrived at the following code:
<script>
$(".altoContrase").click(
$("div").each(function() {
$(this).css({
'background': '#000', 'color': '#FFF'
})
});
</script>
Why isn’t it working? What should I do to be able to do this? Besides, in this jQuery code I can’t change the color of links.
If you can do with pure javascript what is the idea of doing in jQuery? time wasting. Or have something that is not working?
– Sergio
Is this jQuery code correct? (i.e. it’s the same code you have on your page? it’s missing a parenthesis there, and the way to call the
click
is passing a function - like you did with theeach
)– mgibsonbr
The idea of doing in jquery is because javascript is giving problem in changing some things in tables.
– Lucas Vilela
jQuery is Javascript. Explaining the problem you have in javascript will be a more important question.
– Sergio
I’m still stirring, I couldn’t do anything with jquery. So I should do : $(". altoContrase"). click( Function() { and dps continue as they were ?
– Lucas Vilela
@Sergio with the javascript code I can’t change the settings of the tables. Making: var tables= general.getelementsbytagname("table"); for (var i = 0; i < tables.length; i++) { tables[i].style.background= '#000'; tables[i].style.color= '#FFF'; } does not work, the table does not change. I also tried to do the same but with TR but tbm it didn’t work
– Lucas Vilela
@Lucasvilela I think it is better to open a separate question for this, since it has nothing to do with the current question. By the way, I don’t see any problem using jQuery even when you can do it in pure Javascript, because jQuery code is usually more concise. But if you have a solution ready, it’s actually more business to solve its problems than to introduce a heavy framework just for that.
– mgibsonbr