jquery 图像地图悬停效果

发布于 2024-11-08 17:30:28 字数 920 浏览 0 评论 0原文

我创建了一个图像地图,我想在鼠标悬停时突出显示地图区域。为了实现这一点,我编写了以下代码。

JS:

<script type="text/javascript">
        $(document).ready(function(){
            $('#Map area').hover(function(){
                alert('alert');
                $(this).css('border','1px solid #FF0000');
            });
        });
</script>

html:

<div id="mainmap"><img src="images/map.jpg" alt="" usemap="#Map"/></div>
<map name="Map" id="Map">
        <area shape="poly" coords="21,326,203,316,220,141,52,153,48,182,37,193,29,220,27,241,28,255,33,259" href="#" alt="1" />
        <area shape="poly" coords="31,405,209,410,225,427,336,427,337,360,200,359,208,317,21,326,15,344,41,360" href="#" alt="11" />
</map>

当我将鼠标悬停在一个区域上时,它会警报两次,而我认为它应该警报一次。另外 .css() 函数似乎不适用于 this 选择器。

请指导我该如何修复

I have created an image map and i want to highlight maps areas on mouse over. To achieve this i wrote following code.

JS:

<script type="text/javascript">
        $(document).ready(function(){
            $('#Map area').hover(function(){
                alert('alert');
                $(this).css('border','1px solid #FF0000');
            });
        });
</script>

html:

<div id="mainmap"><img src="images/map.jpg" alt="" usemap="#Map"/></div>
<map name="Map" id="Map">
        <area shape="poly" coords="21,326,203,316,220,141,52,153,48,182,37,193,29,220,27,241,28,255,33,259" href="#" alt="1" />
        <area shape="poly" coords="31,405,209,410,225,427,336,427,337,360,200,359,208,317,21,326,15,344,41,360" href="#" alt="11" />
</map>

When i mouse over an area it alerts twice while i think it is supposed to alert once. Also .css() function does not seem to work with this selector.

Please guide how can i fix

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜声 2024-11-15 17:30:28

您需要了解

的含义,它是图像点击区域的简单定义

$(this).css('border','1pxsolid #FF0000' ); 你告诉让

有一个实心边框,但

就像 ; 元素,它只包含定义。

要在这些区域中制作边框,您确实需要 2 张图像。

You need to understand what <map> means, it's a simple definition on the click areas of an image

saying $(this).css('border','1px solid #FF0000'); you are telling to make the <map> have a solid border, but <map> is just like the <head> elements, it only contains definitions.

To make the borders in the areas, you really need to have 2 images instead.

囚你心 2024-11-15 17:30:28

看来当你在地图区域内移动鼠标时,会弹出一个警告,此时,你的鼠标已经离开了地图区域,所以触发了onmouseleave事件,所以有两个<代码>警报。如果你用console.log替换它们,应该没问题。在这里测试一下:http://jsfiddle.net/8ZXb9/

It seems that when you move mouse in the map area, an alert will pop out, at this point, your mouse is out of the map area, so the onmouseleave event was triggered, so there are two alerts. if you replace them with console.log, it should be fine. Test it here: http://jsfiddle.net/8ZXb9/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文