使用 JavaScript(或 jQuery)确定点击了地图 (imageMap) 中的哪个区域

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10978103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:41:46  来源:igfitidea点击:

Determine which Area in a Map (imageMap) was clicked using JavaScript (or jQuery)

javascriptjqueryhtml

提问by Mike Sav

I'm writing a mock up (using HTML, JS/jQuery) and CSS for a client which involves having a single image (of an interface) with an Map applied to it. For reasons I won't explain when a certain area is clicked an action is performed which includes an animation, then changing the image src(so it looks like it is moving between interfaces) and then I apply a different imagemap to the image (perhaps I should say <img>tag)

我正在为一个客户端编写一个模型(使用 HTML、JS/jQuery)和 CSS,其中涉及一个应用了 Map 的单个图像(一个界面)。出于某些原因,我不会解释当单击某个区域时会执行一个动作,其中包括动画,然后更改图像src(因此它看起来像是在界面之间移动),然后我将不同的图像映射应用于图像(也许我应该说<img>标签)

The client asked for this, I know it seems like madness but I don't have any mockup tools to perform the animations, etc, etc...

客户要求这样做,我知道这看起来很疯狂,但我没有任何模型工具来执行动画等……

I've noticed a pattern and I could refactor my code so it is easier to extend and maintain. However I was wondering, say I have the following HTML:

我注意到一个模式,我可以重构我的代码,以便更容易扩展和维护。但是我想知道,假设我有以下 HTML:

<img src="claas_ipad3.jpg" USEMAP="#claas_ipad3" width="1130" height="871" alt="" border="0" id="mainPage">
    <map name="claas_ipad3">
      <area shape="rect" coords="181,249,255,341" href=""  alt="" id="Contacts">
      <area shape="rect" coords="345,251,454,341" href=""  alt="" id="Appointments">
      <area shape="rect" coords="533,256,576,311" href=""  alt="" id="Maps">
      <area shape="rect" coords="686,255,785,344" href=""  alt="" id="Tasks">
      <area shape="rect" coords="835,246,973,348" href=""  alt="" id="Products">
      <area shape="rect" coords="176,412,278,513" href=""  alt="" id="Reports">
      <area shape="rect" coords="330,411,457,513" href=""  alt="" id="PriceTable">
      <area shape="rect" coords="502,399,637,518" href=""  alt="" id="SalesCycle">
      <area shape="rect" coords="677,398,808,519" href=""  alt="" id="MetaData">
      <area shape="rect" coords="844,408,970,510" href=""  alt="" id="Settings">
      <area shape="rect" coords="173,545,283,662" href=""  alt="" id="Vids">
      <area shape="rect" coords="328,558,461,652" href=""  alt="" id="Web">
      <area shape="rect" coords="491,559,626,666" href=""  alt="" id="Files">
    </map>

Is it possible for me to determine using JavaScript or jQuery if an area was clicked? Then I could identify the ID and perform the correct actions. What I currently have is a lot of different conditions like so...

如果单击了某个区域,我是否可以确定使用 JavaScript 或 jQuery?然后我可以识别 ID 并执行正确的操作。我目前拥有的是很多不同的条件,例如......

 $("#Contacts").click(function(event){
            event.preventDefault();

            // okay lets show the contacts interface
            $("#mainPage").fadeOut(300, function(){
                $(this).attr('src','claas_ipad3_1.jpg').bind('onreadystatechange load', function(){
                    if (this.complete){
                        $(this).fadeIn(300);
                        $(this).attr('USEMAP','#claas_ipad_1');
                    }
                    });
                });
            });

However if I know which area was clicked (by determining the id) I can apply array values into a generic function rather than binding to the map areas specifically.

但是,如果我知道点击了哪个区域(通过确定 id),我可以将数组值应用到通用函数中,而不是专门绑定到地图区域。

I was thinking I could determine the id of what was clicked something like this:

我在想我可以确定点击的内容的 id 是这样的:

$(this).live('click', function () {
    alert($(this).attr('id')); 
});

however this will affect my whole page, which isn't a problem but I know it won't work.

但是这会影响我的整个页面,这不是问题,但我知道它不起作用。

Sorry if I haven't explained myself well or my wording is poor. I can extend or reword if desired.

对不起,如果我没有很好地解释自己或者我的措辞很糟糕。如果需要,我可以扩展或改写。

Thanks

谢谢

UPDATE

更新

I have solved this, if I apply an ID to the Map using the following will return the ID

我已经解决了这个问题,如果我使用以下方法将 ID 应用到 Map 将返回 ID

 $("#Map1 area").click( function () {
        alert($(this).attr('id')); // this
    });

采纳答案by jantimon

There are different approaches. I guess the easiest would be this one:

有不同的方法。我想最简单的是这个:

$("map[name=claas_ipad3] area").live('click', function () {
    alert($(this).attr('id')); 
});

Note that as of jQuery 1.7, the .live() method is deprecated and you should use .on() to attach event handlers:

请注意,从 jQuery 1.7 开始,不推荐使用 .live() 方法,您应该使用 .on() 附加事件处理程序:

$("map[name=claas_ipad3] area").on('click', function () {
    alert($(this).attr('id')); 
});