JavaScript objects can be nested using the dot operator as the following:
var cellsColl = tableElem.rows[0].cells;
for (var i = 0; i < cellsColl.length; i++)
{
cellsColl[i].style.backgroundColor = '#FF0000';
}
If you do multiple operations on the above construct, it is better to define a variable to reference it because each dot operator causes an operation to retrieve that property. For example, if you need to process table cells inside a for loop, define a local variable to reference the cells collection and use it instead as follows:
for (var i = 0, loopCnt = cellsColl.length; i < loopCnt; i++)
Even though the above code references the cells collection through the local
variable, it still checks the length property of the cells collection in each step of the loop. Therefore, it is better to store cellsColl.length property in a local variable by getting its value once as follows:
0 comments:
Post a Comment