简体   繁体   中英

Redefine click selector jQuery

I am trying to make jQuery update the values in the selector after they are passed. What I mean is this.

I am passing the selector like this.

var items = ['.item-1','.item-2'...];
$(items[1]).click(function(){....});

And then in the end of the function I change the order of the items array.

var items = ['.item-1','.item-2'...];
$(items[1]).click(function(){
    // ... function
    items = ['.item-3', '.item-1' ...];
});

Now the problem is that the function is binded to the inital items[1], so my changing of the array does not really matter. I am pretty sure there should be a not too complicated solution, so can you please point me in the right direction ?

You could one method in combination with a recursion schema

var items = ['.item-1','.item-2','.item-3'];
$(items[1]).one('click', clickHandler);

function clickHandler() {
    items = //re-sort items;
    $(items[1]).one('click', clickHandler);
}

Take into account that arrays index is zero based so you're using the second item and not the first when doing items[1] .

I think you have to use a recursive method.

It could be:

   var items = ['.item-1','.item-2'];
    function redefine(){
        $(items[1]).click(function(){
            items = ['.item-3', '.item-1'];
            $(this).unbind("click");
            redefine();
        });
    }
    redefine()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM