复制事件处理程序到
;一个元素到另一个元素

发布于 2024-11-28 02:11:18 字数 792 浏览 6 评论 0原文

我不认为任何类似的现有问题可以回答这个问题。

我正在开发一个插件,它将把 转换为具有两个切换状态的

。使用的基本思想是:

$('div .checkboxContainer').prettyBox();

插件本身的伪代码是:

$.fn.prettyBox = function(){
    return this.each(function(){
        $(this).find(':checkbox').each(function(){
           .. grab all event handlers on the current <input>
           .. create a new <div>
           .. attach all of the <input>'s event handlers to the <div>
           .. hide the <input>
           .. place the <div> where the <input> used to live
        });  
    };
};

其他提出类似问题的人一直关心复制单个事件,例如 click 处理程序。为了保持插件的灵活性,我认为我的代码循环遍历附加到输入的所有内容并将其复制过来很重要。

I don't believe any similar existing questions answer this one.

I am developing a plugin that will turn <input type='checkbox' /> into a <div> with two toggle states. The basic idea for use is:

$('div .checkboxContainer').prettyBox();

The pseudo code for the plugin itself is:

$.fn.prettyBox = function(){
    return this.each(function(){
        $(this).find(':checkbox').each(function(){
           .. grab all event handlers on the current <input>
           .. create a new <div>
           .. attach all of the <input>'s event handlers to the <div>
           .. hide the <input>
           .. place the <div> where the <input> used to live
        });  
    };
};

Others who have asked similar questions have been concerned with copying single events, such as a click handler. To maintain plugin flexibility, I think it's important that my code loop through everything attached to the input and copy it over.

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

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

发布评论

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

评论(2

腹黑女流氓 2024-12-05 02:11:18

试试这个

$.fn.prettyBox = function(){
    return this.each(function(){
        $(this).find(':checkbox').each(function(){
           //Grabs all the events
           var events = $(this).data("events");
           var $div = $("<div />");

           //Loop through all the events
           $.each(events, function(i, event) {
             //Loop through all the handlers attached for a event
             $.each(event, function(j, h) {
               //Bind the handler with the event 
               $div.bind(i, h.handler);
             });
           });

           $(this).hide().after($div);
        });  
    };
};

Try this

$.fn.prettyBox = function(){
    return this.each(function(){
        $(this).find(':checkbox').each(function(){
           //Grabs all the events
           var events = $(this).data("events");
           var $div = $("<div />");

           //Loop through all the events
           $.each(events, function(i, event) {
             //Loop through all the handlers attached for a event
             $.each(event, function(j, h) {
               //Bind the handler with the event 
               $div.bind(i, h.handler);
             });
           });

           $(this).hide().after($div);
        });  
    };
};
清旖 2024-12-05 02:11:18

我认为如果事件也使用 jQuery 绑定,您可以使用 $('#elem').data("events")

来源: https://stackoverflow.com/questions/2518421/jquery-find-events-handlers-registered-with-an-object

I think if the events are also bound using jQuery, you can get all events using $('#elem').data("events")

Soure: https://stackoverflow.com/questions/2518421/jquery-find-events-handlers-registered-with-an-object

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