<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>定义函数</title>
<script type="text/javascript">
alert(document.getElementsByClassName("sale").length);
</script>
</head>
<body>
<h1>what to buy</h1>
<p title="a gentle reminder">Don't forget to bu this stuff</p>
<ul id="purchases">
<li>aaaaaaa</li>
<li class="sale">bbb</li>
<li class="sale important">milk</li>
</ul>
</body>
</html>
这length为什么提示是0?
0是因为执行顺序问题,你把script标签整体移动到body结尾标记前