質問

Im trying to unbind a click call on element and than unbind the unbind. I know i can use a handler to handle each and each click, but im using unbind on too many element by using for example:

$(".classname").unbind('click');

and so i can allow to myself to just write it 1 by one, any other esthetic way and way more efficient than writing it 1 by 1 ? Thanks.

EDIT:

detailed example : i got many buttons which all have same css class, when i press 1 specific button i need all of the rest to be unbinded from clicking them. after the button i pressed finishes his purpese i need to re-bind all the click events again. Thats is what i ment when i said unbind the unbind.

役に立ちましたか?

解決

If you unbind your buttons to avoid multi-calling of the function, why didn't you define a global boolean variable like inProcess. You check if it was true or false in the beginning of your function and it's done!

Like this :

var inProcess = false;
function myFunction() {
    if (!inProcess) {
        inProcess = true;
        // some logic code here
        inProcess = false;
    }
    return;
}

It's almost the same process, but, it's lighter for your browser than "bind"/"unbind" multiple time.

他のヒント

It seems to me that you want a handful of the elements to have click events and not the rest. To achieve this just give the elements an arbitrary class name so you can use this as a jQuery selector.

<p class="classname">Some element</p>
<p class="classname">Some element</p>
<p class="classname">Some element</p>
<p class="classname arbitraryclassname">Some element</p>
<p class="classname arbitraryclassname">Some element</p>
<p class="classname arbitraryclassname">Some element</p>

Then in Jquery use

$(".arbitraryclassname").bind('click', function() { 
     // Some logic
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top