如何获取图像地图中鼠标悬停的图像ID

发布于 2024-08-15 14:17:40 字数 559 浏览 3 评论 0原文

假设您有两个使用相同图像贴图的图像。

<img src="/test/image1.jpg" id="image1" useMap="map-name">
<img src="/test/image2.jpg" id="image2" useMap="map-name">

<map name="map-name">
  <area shape="rect" coords="0,0,82,126" alt="Sun" onmouseover="testFunction();"/>
  <area shape="circle" coords="90,58,3"  alt="Mercury" onmouseover="testFunction();"/>
  <area shape="circle" coords="124,58,8" alt="Venus" onmouseover="testFunction();"/>
</map>

有什么方法可以获取 testFunction() 内部图像的 ID(本例中为 image1|image2)?

Let's say you have two images that use the same image map.

<img src="/test/image1.jpg" id="image1" useMap="map-name">
<img src="/test/image2.jpg" id="image2" useMap="map-name">

<map name="map-name">
  <area shape="rect" coords="0,0,82,126" alt="Sun" onmouseover="testFunction();"/>
  <area shape="circle" coords="90,58,3"  alt="Mercury" onmouseover="testFunction();"/>
  <area shape="circle" coords="124,58,8" alt="Venus" onmouseover="testFunction();"/>
</map>

Is there any way to get the ID of the image (image1|image2 in this case) inside of the testFunction()?

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

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

发布评论

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

评论(1

看海 2024-08-22 14:17:40

是的。最简单的方法是使用像 jQuery 这样的库来挂钩事件,而不是使用 onmouseover。如果你这样做,你基本上会做类似的事情:

$("AREA").mouseOver(function (e) {
  var id = $(e.target).attr("id");
}

如果你不这样做,那么......我们在 testFunction 的定义中使用“this”,祝你好运(因为你将处理跨浏览器不兼容的乐趣)。

Yes. The easiest way is to use a library like jQuery to hook up the event, instead of using onmouseover. If you do that, you'll basically do something like:

$("AREA").mouseOver(function (e) {
  var id = $(e.target).attr("id");
}

If you don't do that then ... well play around with "this" inside your definition of testFunction, and good luck (because you'll get to deal with the joy of cross-browser incompatibility).

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