如何在mootools(1.1)中单击按钮时获取元素的id
我有一堆带有非预先确定的 id 的链接,如下所示:
<a href="#" class="remove_pid" id="pid_123">Remove 123</a>
<a href="#" class="remove_pid" id="pid_234">Remove 234</a>
<a href="#" class="remove_pid" id="pid_567">Remove 567</a>
<a href="#" class="remove_pid" id="pid_890">Remove 890</a>
我有一个像这样的事件处理程序:
$$('.remove_pid').addEvents({
'click': removePid
});
它调用此函数
function removePid(event)
{
alert('yo what is my element id???');
}
所以问题是如何在函数removePid() 中获取元素 id?
更新:
@Aishwar,event.target.id 似乎在以下情况下工作,但不是专门针对我的情况
<a href="#" class="remove_pid"><img src="/123.jpg" id="pid_123"></a>
更新 2:
我认为这是无关紧要的,但不是文本“删除 123”我实际上有一个像这样的图像:
<a href="#" class="remove_pid" id="pid_123"><img src="/123.jpg"></a>
所以,感谢@Dimitra 指出它。我对投票感到惊讶,但很高兴地说我可能应得的。
I have a bunch of links with non-pre-determined id's like so:
<a href="#" class="remove_pid" id="pid_123">Remove 123</a>
<a href="#" class="remove_pid" id="pid_234">Remove 234</a>
<a href="#" class="remove_pid" id="pid_567">Remove 567</a>
<a href="#" class="remove_pid" id="pid_890">Remove 890</a>
I have an event handler like so:
$('.remove_pid').addEvents({
'click': removePid
});
which calls this function
function removePid(event)
{
alert('yo what is my element id???');
}
So the question is how do i get the element id in the function removePid()?
UPDATE:
@Aishwar, event.target.id seems to work in the following case, but not specifically in my case
<a href="#" class="remove_pid"><img src="/123.jpg" id="pid_123"></a>
UPDATE 2:
I thought it was inconsequential, but instead of the text "Remove 123" I actually have an image like so:
<a href="#" class="remove_pid" id="pid_123"><img src="/123.jpg"></a>
So, thanks for @Dimitra for pointing it out. I was surprised with the de-vote but am happy to say i probably deserve it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我没有使用 mootools 的经验。但我猜你可以在
removePid
中按照这些思路做一些事情:I do not have experience working with mootools. But I would guess you can just do something along these lines, in
removePid
:根据最终更新中发布的标记:
http://www.jsfiddle.net/dimitar/Sr8LC/
使用命名函数并保留事件:
在两个函数中,
this
将引用触发元素(锚点),因此您可以读取它的属性等。this .getFirst()
将引用图像(如果需要的话)。as per the markup posted in the FINAL update:
http://www.jsfiddle.net/dimitar/Sr8LC/
to use a named function and keep the event:
within both functions,
this
will refer to the trigger element (the anchor), so you can read it's property, etc.this.getFirst()
will reference the image (if you want it).我想我找到了:
这适用于 FF 3.6
Think I found it:
This works in FF 3.6