javascript代码为什么不能实现效果呢?

来源:百度知道 编辑:UC知道 时间:2024/06/07 08:58:37
<script type="text/javascript">
var aa = document.getElementsByTagName("area");
alert(aa.length);
</script>
<img src="1-8.jpg" width="522" height="1640" border="0" usemap="#Map">
<map name="Map">
<area shape="rect" name="ww" coords="49,166,103,203" href="#">
<area shape="rect" name="ww" coords="129,168,191,205" href="#">
<area shape="rect" name="ww" coords="229,168,296,206" href="#">
<area shape="rect" name="ww" coords="323,171,394,206" href="#">
<area shape="rect" name="ww" coords="44,245,167,282" href="#">
<area shape="rect" name="ww" coords="2

那是因为你把javascript放到html的上方去了,javascript是解释型语言,浏览器加载到哪就解析哪,当浏览器加载到var aa =xxx的时候,你的html内容还没加载,当然就取不到内容了,你应该换一下顺序,这样:
<img src="1-8.jpg" width="522" height="1640" border="0" usemap="#Map">
<map name="Map">
<area shape="rect" name="ww" coords="49,166,103,203" href="#">
<area shape="rect" name="ww" coords="129,168,191,205" href="#">
<area shape="rect" name="ww" coords="229,168,296,206" href="#">
<area shape="rect" name="ww" coords="323,171,394,206" href="#">
<area shape="rect" name="ww" coords="44,245,167,282" href="#">
<area shape="rect" name="ww" coords="220,248,341,284" href="#">
<area sh