如何在mootools(1.1)中单击按钮时获取元素的id

发布于 2024-10-02 06:10:54 字数 1041 浏览 0 评论 0原文

我有一堆带有非预先确定的 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 技术交流群。

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

发布评论

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

评论(3

一萌ing 2024-10-09 06:10:54

我没有使用 mootools 的经验。但我猜你可以在 removePid 中按照这些思路做一些事情:

var element = event.srcElement || event.target
element.id // is the element's id, element is the DOM element itself

I do not have experience working with mootools. But I would guess you can just do something along these lines, in removePid:

var element = event.srcElement || event.target
element.id // is the element's id, element is the DOM element itself
于我来说 2024-10-09 06:10:54

根据最终更新中发布的标记:

http://www.jsfiddle.net/dimitar/Sr8LC/

$('.remove_pid').addEvents({
    'click': function(e) {
        new Event(e).stop();
        var id = this.getProperty("id");
        alert(id);
        alert(id.replace("pid_", ""));
    }
});

使用命名函数并保留事件:

var removeProduct = function(e) {
    new Event(e).stop();
    var id = this.getProperty("id");
    alert(id);
    alert(id.replace("pid_", ""));
};

$('a.remove_pid').each(function(el) {
    el.addEvents({
        'click': removeProduct.bind(el)
    });
});

在两个函数中,this 将引用触发元素(锚点),因此您可以读取它的属性等。this .getFirst() 将引用图像(如果需要的话)。

as per the markup posted in the FINAL update:

http://www.jsfiddle.net/dimitar/Sr8LC/

$('.remove_pid').addEvents({
    'click': function(e) {
        new Event(e).stop();
        var id = this.getProperty("id");
        alert(id);
        alert(id.replace("pid_", ""));
    }
});

to use a named function and keep the event:

var removeProduct = function(e) {
    new Event(e).stop();
    var id = this.getProperty("id");
    alert(id);
    alert(id.replace("pid_", ""));
};

$('a.remove_pid').each(function(el) {
    el.addEvents({
        'click': removeProduct.bind(el)
    });
});

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).

你曾走过我的故事 2024-10-09 06:10:54

我想我找到了:

function removePid(event)
{
    //alert('yo what is my element id???');
    $(event.target).getParent('a').getProperty('id');
}

这适用于 FF 3.6

Think I found it:

function removePid(event)
{
    //alert('yo what is my element id???');
    $(event.target).getParent('a').getProperty('id');
}

This works in FF 3.6

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