首先,我的html内容如下:<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>MyXhtml.xhtml</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="example.js">
</script>
</head>
<body>
<h1>SnapShots</h1>
<ul>
<li>
<a href = "images/1.jpg" title = "1" onclick = "showPic(this); return false;">1</a>
</li>
<li>
<a href = "images/2.jpg" title = "2" onclick = "showPic(this); return false;">2</a>
</li>
<li>
<a href = "images/3.jpg" title = "3" onclick = "showPic(this); return false;">3</a>
</li>
</ul>
<p id = "description">choose an image</p>
<img id="placeholder" src = "images/3.jpg" alt="my image gallery"/>
</body>
</html>我的example.js内容如下:for(var propName in window) {
document.write(proName);
document.write("<br/>");
}function showPic(whichpic) {
var source = whichpic.getAttribute("href");
var placeholder = document.getElementById("placeholder");
placeholder.setAttribute("src", source);
var text = whichpic.getAttribute("title");
var description = document.getElementById("description");
description.lastChild.nodeValue = text;
}function countBodyChildren() {
var body_element = document.getElementsByTagName("body")[0];
alert(body_element.childNodes.nodeType);
alert(body_element.nodeType);
}window.onload = countBodyChildren;function sum(num1, num2) {
return num1 + num2;
}
alert(sum(10, 10));var anothersum = sum;
alert(anothersum(10,10));sum = null;
alert(anothersum(10, 10));
alert(sum(10, 10));为什么js中的function sum及下面的不能执行,把function sum及以下的写到js文件的最上面就能执行?哪位大侠帮忙解答一下,谢谢!
<head>
<title>MyXhtml.xhtml</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="example.js">
</script>
</head>
<body>
<h1>SnapShots</h1>
<ul>
<li>
<a href = "images/1.jpg" title = "1" onclick = "showPic(this); return false;">1</a>
</li>
<li>
<a href = "images/2.jpg" title = "2" onclick = "showPic(this); return false;">2</a>
</li>
<li>
<a href = "images/3.jpg" title = "3" onclick = "showPic(this); return false;">3</a>
</li>
</ul>
<p id = "description">choose an image</p>
<img id="placeholder" src = "images/3.jpg" alt="my image gallery"/>
</body>
</html>我的example.js内容如下:for(var propName in window) {
document.write(proName);
document.write("<br/>");
}function showPic(whichpic) {
var source = whichpic.getAttribute("href");
var placeholder = document.getElementById("placeholder");
placeholder.setAttribute("src", source);
var text = whichpic.getAttribute("title");
var description = document.getElementById("description");
description.lastChild.nodeValue = text;
}function countBodyChildren() {
var body_element = document.getElementsByTagName("body")[0];
alert(body_element.childNodes.nodeType);
alert(body_element.nodeType);
}window.onload = countBodyChildren;function sum(num1, num2) {
return num1 + num2;
}
alert(sum(10, 10));var anothersum = sum;
alert(anothersum(10,10));sum = null;
alert(anothersum(10, 10));
alert(sum(10, 10));为什么js中的function sum及下面的不能执行,把function sum及以下的写到js文件的最上面就能执行?哪位大侠帮忙解答一下,谢谢!
<head>
<title>MyXhtml.xhtml</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
.........
</body>
<script type="text/javascript" src="example.js"></script>
</html>
哥们,这样写吧,不然像你那样写的,body没有加载完就开始执行JS了,找不到DOM,就找错,不好
1。书上写着函数名实际上是一个指向函数对象的指针,函数是对象,那么设置sum = null,这个sum指针就与函数脱离关系了啊?还有前面那个执行顺序的问题是怎么回事呢?
2.js中只执行到window.onload = countBodyChildren;后面的js都没有执行,为什么呢?
请大虾们帮忙解答一下,谢谢!
A=null;
A();
测试下就知道了。报错!之所以能看到对话框是anothersum报出的。
document.write(proName);
document.write("<br/>");
}
propName proName