js执行的顺序问题

hero_winner hero_winner 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
首先,我的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文件的最上面就能执行?哪位大侠帮忙解答一下,谢谢!
...全文

相关推荐

评论 10
收藏 已收藏
分享
返回顶部
hero_winner
JavaScript 87,984 成员 224,681 内容