开发者

jQuery - How to write a plugin that will automatically call on future elements

开发者 https://www.devze.com 2023-02-12 13:26 出处:网络
A simple plugin jQuery.fn.cleanHouse = function() { //do stuff } $(\'.dirty\').cleanHouse(); How can we make sure future elements $(\'.dirty\') will automatically call 开发者_StackOverflow中文版cl

A simple plugin

jQuery.fn.cleanHouse = function() {
    //do stuff
}

$('.dirty').cleanHouse();

How can we make sure future elements $('.dirty') will automatically call 开发者_StackOverflow中文版cleanHouse() ?

I'm looking solution do something with the plugin itself


is jquery's live() function what you need?

http://api.jquery.com/live/

or this

jQuery - Fire event if CSS class changed


The problem with .live() is that you can only use it in connection with an event, and there is no built-in event that fires when you add elements to the DOM.

But here's a detailed explanation on how you make that happen with a custom "create" event: http://www.erichynds.com/jquery/jquery-create-event/


I think what you are looking for is a combination, try something like this:

$.fn.cleanHouse = function() {
    return $(this).live('click', function() { 
        //do stuff
    });
}
$('.dirty').cleanHouse();


It's not possible to achieve this. The only way the plugin can get called in the future is through an event.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号