HTML。Javascript,程序解释

来源:百度知道 编辑:UC知道 时间:2024/05/29 23:54:15
<html>
<head>
<title>JavaScript示例,访问DOM结点</title>
</head>
<body>

<p>Hello World!</p>
<p>The DOM is very useful!</p>
<p>This example demonstrates the <b>length</b> property.</p>

<script type="text/javascript">
x=document.getElementsByTagName("p");
for (i=0;i<x.length;i++) {
document.write(x[i].innerHTML);
document.write("<br />");
}

document.write(x[2].firstChild.nodeValue);

</script>
</body>
</html>

希望高手能具体解释其中每一行的执行情况

<script type="text/javascript">
x=document.getElementsByTagName("p");//将所有<p>标签对象的集合赋值给x
for (i=0;i<x.length;i++) { //遍历所有的<p>标签
document.write(x[i].innerHTML);//将每个<p>标签内的内容打印到页面上
document.write("<br />");//每打印一个一换行
}

document.write(x[2].firstChild.nodeValue);//将第三个<p>标签中的第一个节点的值打印出来即"This example demonstrates the "

</script>

<script type="text/javascript">//声明是javascript脚本编程语言
x=document.getElementsByTagName("p");//document是整个html文档的意思。整句就是返回文档中拥有指定标签名“P”的所有元素
for (i=0;i<x.length;i++) { //x.length是指x数组的长度。
document.write(x[i].innerHTML);//输出每个"p"标签内的内容
document.write("<br />");//输出HTML的换行标记,且执行换行。
}

document.write(x[2].firstChild.nodeValue);//将第三个“P”标签中的第一个节点的值打印出来(也就是输出出来)

<html>
<head>
<title>JavaScript示例,访问DOM结点&