Plain JavaScript solution:
You cannot use getElementById()
in this case since its purpose is only to query id attributes, but you can use getElementsByTagName()
in context of #menu
:
var m = document.getElementById('menu');
// Get all <li> children of #menu
var lis = m.getElementsByTagName('li');
// Loop over them
for (var i=0; i<lis.length; i++) {
// Get all <a> children of each <li>
var atags = lis[i].getElementsByTagName('a');
for (var a = 0; a<atags.length; a++) {
// And set their color in a loop.
atags[a].style.color = 'blue';
// or change some other property
atags[a].style.height = '25%';
}
}
jQuery Solution:
If you are able to use jQuery, this becomes exceedingly simpler:
$('#menu li a').css('color', 'blue');
$('#menu li a').css('height', '10%');
Otherwise the necessary DOM traversal is much more complicated unless you only want to support modern browsers which have thequerySelectorAll
method. – ThiefMaster♦ Jul 26 '12 at 23:54