2016-07-25 109 views
0

我是HTML初學者,正在編寫一個網格,將畫布分割成網格。用戶可以將鼠標懸停在網格內的矩形上並突出顯示它們。將.txt文件讀入HTML映射圖

我有很多座標的.txt文件中存儲的矩形(每行有4個座標用空格分隔),我希望逐行讀入文件並將它們作爲變量輸入到我的代碼中蟒蛇。

<area shape="rect" coords="xmin,xmax,ymin,ymax" href="#"...> 

任何意見/在哪裏指向我非常感謝,因爲有太多的座標我手動輸入!

回答

0

你在找什麼是AJAX。你可以使用像下面這樣的東西。

var xhr = new XMLHttpRequest(); 
xhr.open("GET", "coords.txt", true); 

xhr.onload = function(e) { 
    if(this.status == 200) { 
     // get text contents 
     var coords = this.responseText.split("\n"); 
     coords.forEach(function(coord) { 
      // create new area element 
      var area = document.createElement("area"); 
      area.shape = "rect"; 
      area.coords = coord.replace(/ /g,","); // replace spaces with commas 
      area.href = "#"; 

      // get your map element somehow 
      document.getElementById("myMap").appendChild(area); 
     }); 
    } 
}; 

xhr.send();