获取调用函数的元素的 ID
如何获取调用 JS 函数的元素的 ID?
body.jpg 是一张狗的图像,当用户将鼠标指向屏幕上身体的不同部位时,会显示放大的图像。区域元素的 ID 与图像文件名减去文件夹和扩展名相同。
<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>
我已经完成了研究,并把 Stack Overflow 作为最后的手段。我更喜欢不涉及 jQuery 的解决方案。
How can I get the ID of an element that called a JS function?
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>
I've done my research and have come to Stack Overflow as a last resort. I'm prefer a solution that doesn't involve jQuery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
对于其他意外获取 Window 元素的人来说,这是一个常见的陷阱:
实际上将
this
范围限定为 Window 对象。相反:按预期传递
a
对象。 (nop() 只是任何空函数。)For others unexpectedly getting the Window element, a common pitfall:
which actually scopes
this
to the Window object. Instead:passes the
a
object as expected. (nop() is just any empty function.)如果你像这样设置一个侦听器:
那么你可以像这样获取 id:
If you setup a listener like this:
then you can get the id like this:
第一种方法:使用
this
发送触发元素第二种方式:使用
this.id
发送触发元素 idFirst Way: Send trigger element using
this
Second Way: Send trigger element id using
this.id
假设您有一个如下所示的按钮:
在该函数中,您可以通过以下方式访问 id:
Let's say, you have a button like this:
In the function, you can reach the id with this way:
调用函数时将对元素的引用传递给函数:
Pass a reference to the element into the function when it is called:
我很惊讶没有人提到在事件处理程序中使用
this
。它可以在现代浏览器中自动运行,并且可以在其他浏览器中运行。如果您使用addEventListener
或attachEvent
来安装事件处理程序,那么您可以将this
的值自动分配给创建的对象事件。此外,以编程方式安装的事件处理程序的用户允许您将 javascript 代码与 HTML 分开,这通常被认为是一件好事。
以下是在纯 javascript 代码中执行此操作的方法:
从 HTML 中删除
onmouseover="zoom()"
并在 javascript 中安装事件处理程序,如下所示:I'm surprised that nobody has mentioned the use of
this
in the event handler. It works automatically in modern browsers and can be made to work in other browsers. If you useaddEventListener
orattachEvent
to install your event handler, then you can make the value ofthis
automatically be assigned to the object the created the event.Further, the user of programmatically installed event handlers allows you to separate javascript code from HTML which is often considered a good thing.
Here's how you would do that in your code in plain javascript:
Remove the
onmouseover="zoom()"
from your HTML and install the event handler in your javascript like this:您可以在事件处理程序中使用“this”:
或者将事件对象传递给处理程序,如下所示:
建议使用attachEvent(对于IE < 9)/ addEventListener(IE9和其他浏览器)来附加事件。上面的示例是为了简洁起见。
You can use 'this' in event handler:
Or pass event object to handler as follows:
It's recommended to use attachEvent(for IE < 9)/addEventListener(IE9 and other browsers) to attach events. Example above is for brevity.
您可以像这样编写处理程序设置:
然后处理程序中的
this
将引用该元素。现在,我要提出警告,我不能 100% 确定当您在标记中有处理程序时会发生什么,很大程度上是因为我还没有看到
标签大约十年左右。我认为它应该为您提供图像标签,但这可能是错误的。
编辑 - 是的,这是错误的 - 你得到的是
标签,而不是
。因此,您必须获取该元素的父元素(地图),然后找到使用它的图像(即
,其“usemap”属性引用地图的名称) 。
再次编辑 - 但这并不重要,因为您想要该区域的“id”durr。抱歉没有正确阅读。
You can code the handler setup like this:
Then
this
in your handler will refer to the element. Now, I'll offer the caveat that I'm not 100% sure what happens when you've got a handler in an<area>
tag, largely because I haven't seen an<area>
tag in like a decade or so. I think it should give you the image tag, but that could be wrong.edit — yes, it's wrong - you get the
<area>
tag, not the<img>
. So you'll have to get that element's parent (the map), and then find the image that's using it (that is, the<img>
whose "usemap" attribute refers to the map's name).edit again — except it doesn't matter because you want the area's "id" durr. Sorry for not reading correctly.
我知道您不想要 jQuery 解决方案,但在 HTML 中包含 javascript 是一个很大的禁忌。
我的意思是你可以这样做,但有很多原因你不应该这样做(如果你想了解详细信息,请阅读不引人注目的 javascript)。
因此,为了其他可能看到这个问题的人的利益,这里是 jQuery 解决方案:
主要好处是您不会将 javascript 代码与 HTML 混合。此外,您只需要编写一次,它将适用于所有标签,而不必分别为每个标签指定处理程序。
额外的好处是每个 jQuery 处理程序都会接收一个包含许多有用数据的事件对象 - 例如事件源、事件类型等,从而使编写您想要的代码变得更加容易。
最后,由于它是 jQuery,因此您无需考虑跨浏览器的问题 - 这是一个主要优点,尤其是在处理事件时。
I know you don't want a jQuery solution but including javascript inside HTML is a big no no.
I mean you can do it but there are lots of reasons why you shouldn't (read up on unobtrusive javascript if you want the details).
So in the interest of other people who may see this question, here is the jQuery solution:
The major benefit is you don't mix javascript code with HTML. Further more, you only need to write this once and it will work for all tags as opposed to having to specify the handler for each separately.
Additional benefit is that every jQuery handler receives an event object that contains a lot of useful data - such as the source of the event, type of the event and so on making it much easier to write the kind of code you are after.
Finally since it's jQuery you don't need to think about cross-browser stuff - a major benefit especially when dealing with events.
我也希望这样的事情发生
所以只需在被调用函数中传递元素的 id 并在我的 js 文件中使用:
i also want this to happen ,
so just pass the id of the element in the called function and used in my js file :