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 63 | Next

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

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

The last example grabs the tag and then loops through its child
nodes and pops an alert with the ID of the element.
That last example covers a few tricks. First, all elements are grabbed that have the tag
name of body. Your HTML document will have only one. Therefore, you can use [0] to
retrieve the first (and only) element from the collection. Next, check to see whether the
node type is equal to 1. Each node type has a number. Element nodes are 1 and text
nodes are 3. Attributes have a node type, but you can??™t retrieve them using the same
methods. Instead, you use either getAttribute() to retrieve a specific attribute or
attributes to access all of them.
Obtaining Elements by Class Name
As mentioned previously, one of the two ways that elements are identified is through IDs
(which you can retrieve through getElementById()) and through class names. Unfortunately,
there isn??™t a getElementsByClassName() in the specification. Since getting elements by their
class name is an effective way to retrieve a set of elements, you??™ll make your own function to
do this.
CHAPTER 2 n HTML, CSS, AND JAVASCRIPT 29
function getElementsByClassName(node, classname)
{
var a = [];
var re = new RegExp('(^| )'+classname+'( |$)');
var els = node.getElementsByTagName("*");
for(var i=0,j=els.length; iif(re.test(els[i].className))a.push(els[i]);
return a;
}
This function takes two parameters: the node from which you want to search and the
class name for which you want to search.


Pages:
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
druga wojna światowa Free English grammar and study guid hotel jelenia góra Russian bride counter strike 1.6