<!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>
<title>测试网页</title>
<script type="text/javascript">
function FunctionTest(){
x = document.getElementsByTagName("p");
document.write(x.length);
document.write("<br />")
for( i = 0; i< x.length; i++){
document.write( i + x[i].firstChild.nodeValue + "<br />");
}
}
</script>
</head>
<body>
<form id="form1" name="ILoveYou" >
<div>
<p>Hello World!</p>
<p>The DOM is very useful!</p>
<p>This example demonstrates the <b>getElementsByTagName</b> method.</p>
<br />
<br />
<input id="testJavaScript" name="testJavaScriptFunction" value="Test JavaScript Function"
onclick="javascript:FunctionTest()"/ >
</div>
</form>
</body>
</html>
请看上面的代码,onclick的注册函数方式有问题吗?当我按了"estJavaScriptFunction"的按钮,页面才显示了3,其他的都没显示,请问怎么回事情呢?
------解决方案--------------------------------------------------------
改成这样:
- JScript code
<script type="text/javascript"> function FunctionTest() { x = document.getElementsByTagName("p"); //document.write(x.length); //document.write("<br />") var str = x.length + "<br />"; ; for (i = 0; i < x.length; i++) { str += i + x[i].firstChild.nodeValue + "<br />" //document.write(i + x[i].firstChild.nodeValue + "<br />"); } document.write(str); } </script>
------解决方案--------------------------------------------------------
document.write()就是相当于页面所有内容替换掉。你到document.write(x.length);的时候相当于原来的页面内容已经不存在了。那后面的当然就不会有效果了