如何使用 jQuery 将事件处理程序动态移动到不同的元素?

发布于 2024-10-02 04:11:02 字数 1105 浏览 0 评论 0原文

jQuery 插件使代码更加可重用。

我之前制作了一个插件,可以打开动态创建的灯箱,从 IMG 标签的属性中提取信息。该插件为图像注册了一个单击事件处理程序。

我最近制作了另一个插件,我想在这些图像上使用它来动态添加叠加层。我遇到的问题是覆盖层完美地完成了它的工作,完全覆盖了图像(并覆盖了在图像上运行单击事件的能力)。

我一直在尝试找到一种方法将事件处理程序从 IMG 复制到新的覆盖层。我发现以下信息给了我一些见解:http: //ejohn.org/apps/workshop/adv-talk/#9

使用上面的代码,我能够检索分配给特定 IMG 元素的数据“events”属性的事件,并将其分配给叠加层元素的数据“事件”属性。不幸的是,似乎就复制而言 - 单击覆盖元素时不会触发该事件。

示例代码(无覆盖,此代码仅用于示例目的):

HTML:

<div class="object1">This is the first object.</div>
<div class="object2">This is the 2nd object.</div>

JavaScript:

$(document).ready(function(){
    $('.object1').click(function(){
        alert('I was clicked.');
    });

    //using 2 params in data() uses a key/value pair
    $('.object2').data('events', $('.object1').data('events'));
    //still no "click" event handler on '.object2', ...
    // ... although the information is stored on the element's data store
});

我希望事件本身也能被注册,但情况似乎并非如此。有办法做到这一点吗?

jQuery plugins make code much more reusable.

I previously made a plugin that would open a dynamically created lightbox that pulled information from an IMG tag's attributes. This plugin registered a click event handler for the image.

I recently made another plugin that I had wanted to use on these images to dynamically add an overlay. The problem I'm having is that the overlay does its job perfectly, completely overlaying the image (and covering the ability to run the click event on the image).

I've been trying to find a way to copy the event handler from the IMG to the new overlay. I found the following information which gave me some insight: http://ejohn.org/apps/workshop/adv-talk/#9

Using the code above, I was able to retrieve the events assigned to the specific IMG element's data "events" property, and assign it to the overlay element's data "events" property. Unfortunately, it seems that is as far as the duplication goes - the event is not fired when the overlay element is clicked.

Example code (no overlay, this code is just for example purposes):

HTML:

<div class="object1">This is the first object.</div>
<div class="object2">This is the 2nd object.</div>

JavaScript:

$(document).ready(function(){
    $('.object1').click(function(){
        alert('I was clicked.');
    });

    //using 2 params in data() uses a key/value pair
    $('.object2').data('events', $('.object1').data('events'));
    //still no "click" event handler on '.object2', ...
    // ... although the information is stored on the element's data store
});

I was hoping that the event itself would also be registered, but this doesn't seem to be the case. Is there a way to do this?

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

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

发布评论

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

评论(2

压抑⊿情绪 2024-10-09 04:11:02

我刚刚想出了这个插件。看起来它做了你想做的事情。
http://plugins.jquery.com/project/copyEvents

I just came up with this plugin. Seems like it does what you intend to do.
http://plugins.jquery.com/project/copyEvents

傲影 2024-10-09 04:11:02

试试这个(查看演示

$(document).ready(function(){
    $('.object1').click(function(){
        alert('I was clicked.');
    });

    $('.object2').click($('.object1').data('events').click[0]);
});

Try this (View demo)

$(document).ready(function(){
    $('.object1').click(function(){
        alert('I was clicked.');
    });

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