質問

Lets say you have 100,000 divs inside a parent div. A jQuery plugin is initialised whenever any of these divs are clicked. If you assign a single class to all these divs and use that as the selector, would it slow performance dramatically? Is there a better way of doing this so that you localise the plugin to each individual div in some way?

役に立ちましたか?

解決 2

Binding events to 100,000 elements would be the performance hit, not the class (assuming the class is there by default, not added by javascript.)

event delegation would be appropriate here.

$(someParent).on("click","div.someclass",function(){
    $(this).somePlugin();
});

他のヒント

Classes should not slow performance dramatically. They only overhead is the time it takes to read over the fact that it has a class which is practically nothing. Actions that actually take processing power only occur when changes are made to the div. This is going to happen regardless of how you set up the click listener.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top