Dave Methvin schrieb:
>>> var counter = 0;
>>> $().click(function(event) { if(counter++ == 5) $(this).unbind(event);
>>> ... });
>>> That was pretty hard to do before.
>>>
>
> I think that example only makes sense if a single element is selected. With
> multiple elements selected, it will unbind one element's event handler on
> the 5th click to any element in the group, but leave handlers bound to the
> others no matter how many more times they were clicked. Changing == to >=
> doesn't help because it's still a collective click count for all selected
> elements.
>
> To get the behavior I think you were trying to get--allow exactly five
> clicks on each selected element--it seems like the count would have to be
> stored in an expando property on each element.
>
Good point. How about...
var counter = {};
$().click(function(event) {
if(!counter[this])
counter[this] = 0;
if(counter[this]++ == 5)
$(this).unbind(event);
});
That, on the other hand, would be worth adding to jQuery core. We could
still rename one() to bindN() with a max count of 1 as default, eg.
bindN("click", 5, myHandler)
If you think this makes sense, please report it as a feature requests.
--
Jörn Zaefferer
http://bassistance.de
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/