<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TEST3.aspx.cs" Inherits="TEST3" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>JavaScript and the DOM</title>
<script language="JavaScript" type="text/javascript">
function test() {
// These first two lines get the DOM tree for the current Web page,
// and then the <html> element for that DOM tree
var myDocument = document;
var htmlElement = myDocument.documentElement;
// What's the name of the html element? "html"
alert("The root element of the page is " + htmlElement.nodeName);
// Look for the head element
var headElement = htmlElement.getElementsByTagName("head")[0];
if (headElement != null) {
alert("We found the head element, named " + headElement.nodeName);
// Print out the title of the page
var titleElement = headElement.getElementsByTagName("title")[0];
if (titleElement != null)
{
// The text will be the first child node of the title element
// var titleText = titleElement.firstChild;
var titleText = titleElement.childNodes[0];
// We can get the text of the text node with nodeValue
if(titleText!=null)
{
alert("The page title is '" + titleText.nodeValue + "'");
}
//alert("The page title is '" + titleElement.nodeValue + "'");
}
// After head is body
var bodyElement = headElement.nextSibling;
while (bodyElement.nodeName.toLowerCase() != "body") {
bodyElement = bodyElement.nextSibling;
}
// We found the body element...
// Remove all the top-level img elements in the body
if (bodyElement.hasChildNodes())
{
var fromElem=bodyElement.getElementsByTagName("form")[0];
for(var i=0; i<fromElem.childNodes.length;i++)
{
var currentNode = fromElem.childNodes[i];
if (currentNode.nodeName.toLowerCase() == "img")
{
fromElem.removeChild(currentNode);
}
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<p>JavaScript and DOM are a perfect match.
You can read more in <i>Head Rush Ajax</i>.</p>
<img src="http://www.headfirstlabs.com/Images/hraj_cover-150.jpg" alt="ff" />
<input type="button" value="Test me!" onclick="test();" />
</form>
</body>
</html>

浙公网安备 33010602011771号