Inside JavaScript
The canHaveChildren Property
When you're constructing your own web pages in JavaScript (using Dynamic HTML or W3C DOM methods ), it can be useful to see whether you're dealing with an element that can contain children or not (for example, <BR> elements cannot). You can see the support for this property in Table 5.9. Table 5.9. The canHaveChildren Property
Here's an examplethis code loops over all elements in a web page and colors them green if they can have children: (Listing 05-04.html on the web site)
<HTML> <HEAD> <TITLE>Using the canHaveChildren Property</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- for (var loopIndex =0; loopIndex <document.all.length; loopIndex++){ if (document.all[loopIndex].canHaveChildren){ document.all[loopIndex].style.color = "green" } } // --> </SCRIPT> </HEAD> <BODY> <H1 NAME="header">Using the canHaveChildren Property</H1> <P NAME="p1">Here's a paragraph.</P> <DIV NAME="div1">Here's a DIV.</DIV> </BODY> </HTML> You can see the results in Figure 5.3 in glorious black and whitealthough you can't tell, all the text in this figure is green. Give this one a tryload it into the Internet Explorer (this property isn't supported in the Netscape Navigator) yourself. Figure 5.3. Using the canHaveChildren property.
|