简体   繁体   English

使用jQuery加载功能的位置区域地图导入外部HTML

[英]Locations area map with jQuery load function importing external HTML

I have a US HTML image map with the states highlighted with coordinates. 我有一张美国HTML图像地图,其中的状态以坐标突出显示。 Each state when clicked will bring up a modal window with facilities locations. 单击每个状态都会弹出一个包含设施位置的模态窗口。

Right now I have it where each state has an ID, the user clicks on the hotspot and then it loads the facilities locations with external HTML via jQuery .load() to lighten the document load. 现在,我在每个州都有一个ID的地方找到它,用户单击热点,然后通过jQuery .load()使用外部HTML加载设施位置,以减轻文档的负担。

Here's an example in the JS 这是JS中的一个例子

$('#Alabama').load('locations/Alabama.html');

and then the container that will load Alabama html in the main document is 然后将在主文档中加载Alabama html的容器是

<div class="overlay" id="Alabama"></div> 

As you can see I have to enter these in for each state. 如您所见,我必须为每个状态输入这些信息。 Obviously this is tedious and probably not the best method. 显然,这很乏味,可能不是最佳方法。 DRY comes to mind but I'm not good at writing Javascript from the ground up. 我想到了DRY,但我并不擅长从头开始编写Javascript。

What would be the best way to access the state IDs without writing each state instance? 在不编写每个状态实例的情况下访问状态ID的最佳方法是什么? I'm thinking a JSON object with a click function that constructs the markup and calls the modal window. 我正在考虑一个带有click函数的JSON对象,该函数构造标记并调用模式窗口。 I'm just not sure where to begin. 我只是不确定从哪里开始。

If it's a modal window, then you could probably just use the same overlay each time a state is clicked. 如果是模式窗口,则每次单击状态时都可以使用相同的叠加层。 So basically just do this... 所以基本上就是这样做...

$(".overlay").load("locations/alabama.html");

A full example might look something more like this though... 完整的示例可能看起来像这样……

$("area").click(function(){
   var url = $(this).attr("href");
   $(".overlay").load(url);
});

You may have to do something to prevent the browser default behavior of navigating on the clicked area. 您可能需要做一些事情来防止浏览器在单击区域上的默认行为。 You could remove the href values and put them somewhere else... 您可以删除href值并将其放在其他位置...

$("area").each(function(i, area){
   $(area).data("old-href", $(area).attr("href");
   $(area).attr("href","#");
});
//Then you do the same as above, but you get your url from the data method.
$("area").click(function(){
   var url = $(this).data("old-href");
   $(".overlay").load(url);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM