获取调用函数的元素的ID [英] Get ID of element that called a function

查看:84
本文介绍了获取调用函数的元素的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取调用JS函数的元素的ID?

How can I get the ID of an element that called a JS function?

body.jpg 是狗的图像,因为用户将他/她的鼠标指向身体不同部位的屏幕周围放大图像显示。区域元素的ID与图像文件名减去文件夹和扩展名相同。

body.jpg is an image of a dog as the user points his/her mouse around the screen at different parts of the body an enlarged image is shown. The ID of the area element is identical to the image filename minus the folder and extension.

<div>
    <img src="images/body.jpg" usemap="#anatomy"/>
</div>

<map name="anatomy">
  <area id="nose" shape="rect" coords="280,240,330,275" onmouseover="zoom()"/>
</map>

<script type="text/javascript">
function zoom()
{
 document.getElementById("preview").src="images/nose.jpg";
}
</script>

<div>
<img id="preview"/>
</div>

我已经完成了我的研究,并作为最后的手段来到SO。我更喜欢不涉及jQuery的解决方案。

I've done my research and have come to SO as a last resort. I'm prefer a solution that doesn't involve jQuery.

推荐答案

将元素的引用传递给函数叫:

Pass a reference to the element into the function when it is called:

<area id="nose" onmouseover="zoom(this);" />

<script>
  function zoom(ele) {
    var id = ele.id;

    console.log('area element id = ' + id);
  }
</script>

这篇关于获取调用函数的元素的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆