SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 64 | Next

Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb

"Accelerated DOM Scripting with Ajax, APIs, and Libraries"

It returns an array of elements with which you can
iterate through.
Taking a look at this function, first you create a regular expression object:
var re = new RegExp('(^| )'+classname+'( |$)');
A regular expression is a syntax for doing string matching. It is very powerful, but also
very confusing. I??™ve hit my head against the wall many times trying to tame a regular
expression. (See later on in this chapter for more information on regular expressions.) This
particular regular expression matches a class name even if there is more than one applied
to the element. The regular expression tries to find the beginning of the class name, which
can either be at the beginning of the string or with a space in front of it. Then, find the class
name you passed into the function. Finally, find the end of the class name by looking for a
space or the end of the string. The ^ matches the beginning of a string; $ matches the end.
The | is similar to JavaScript??™s || and checks to see whether it matches the character on the
right or the character on the left of |.
Next, get all elements of the node that were passed in:
var els = node.getElementsByTagName("*");
The asterisk ("*") being passed into the getElementsByTagName() method discussed earlier
means to return all elements.
Loop through the collection and test whether each class name matches the regular
expression:
for(var i=0,j=els.length; iif(re.


Pages:
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76