将.txt文件读入HTML映射图
问题描述:
我是HTML初学者,正在编写一个网格,将画布分割成网格。用户可以将鼠标悬停在网格内的矩形上并突出显示它们。将.txt文件读入HTML映射图
我有很多坐标的.txt文件中存储的矩形(每行有4个坐标用空格分隔),我希望逐行读入文件并将它们作为变量输入到我的代码中蟒蛇。
<area shape="rect" coords="xmin,xmax,ymin,ymax" href="#"...>
任何意见/在哪里指向我非常感谢,因为有太多的坐标我手动输入!
答
你在找什么是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();