2016-06-11 50 views
0

我正在嘗試使用動態調整大小。我知道一些JavaScript,但它是非常新的。我收到以下錯誤:index.html的:20遺漏的類型錯誤:無法讀取屬性「的getElementsByTagName」空的javascript圖像映射調整大小問題

此錯誤是行:面積= map.getElementsByTagName(「區」),

謝謝Teemu爲腳本Dynamically resizing Image-maps and images

<html> 

<head> 
<link rel="stylesheet" type="text/css" href="PB.css"> 

</head> 

<body> 

<script> 

window.onload = function() { 
    var ImageMap = function (map) { 
      var n, 
       areas = map.getElementsByTagName('area'), 
       len = areas.length, 
       coords = [], 
       previousWidth = 1280; 
      for (n = 0; n < len; n++) { 
       coords[n] = areas[n].coords.split(','); 
      } 
      this.resize = function() { 
       var n, m, clen, 
        x = document.body.clientWidth/previousWidth; 
       for (n = 0; n < len; n++) { 
        clen = coords[n].length; 
        for (m = 0; m < clen; m++) { 
         coords[n][m] *= x; 
        } 
        areas[n].coords = coords[n].join(','); 
       } 
       previousWidth = document.body.clientWidth; 
       return true; 
      }; 
      window.onresize = this.resize; 
     }, 
     imageMap = new ImageMap(document.getElementById('planetbobmap')); 
    imageMap.resize(); 
    return; 
} 

</script> 

<img src="Planet Bob.jpg" alt="PlanetBob" usemap="#planetbobmap" border="0" width="1280" height="720" /> 
<map name="planetbobmap"> 
    <area shape="rect" coords="190,140,435,200" alt="skills" href="Skills.jpg" target="_blank" /> 
    <area shape="rect" coords="890,140,1250,200" alt="projects" href="Projects.jpg" target="_blank" /> 
    <area shape="rect" coords="50,460,440,525" alt="schooling" href="Schooling.jpg" target="_blank" /> 
    <area shape="rect" coords="900,460,1230,525" alt="contact" href="Contact.jpg" target="_blank" /> 
    <area shape="circle" coords="652,352,162" alt="Resume" href="Robert J Norton Resume.pdf" target="_blank" /> 
</map> 

</body> 

</html> 

回答

0

我看到的第一個問題是,你不能訪問使用的document.getElementById(「planetbobmap」)

地圖元素,因爲在地圖元素屬性ID沒有被定義。

嘗試使用此html變更。

<map id = "planetbobmap" name="planetbobmap"> 
+0

而'map'沒有在'function(map)'函數中定義... –

+0

那麼,這個問題就解決了。似乎該腳本仍未改變圖像映射。在添加地圖標識之後,雖然沒有顯示檢查錯誤。 –