开发者

Jquery: What's better? Checking by Global Variable, or by class applied to element?

Would it be better to set a global variable, then check that? or to set a开发者_如何转开发 class on an element, then check that?

what's the benefit of each?

tiny example:

if(something == "myGlobalVariable")
yada yada yada

or

if($('.element').hasClass('foo'))
yada yada yada


With global variables, you'll end up polluting the namespace, which could result in conflicts down the road. Just a bad idea in general.

There is nothing wrong with checking an element to see if it has a certain class. In fact, that is the way many plugins are written to wire by convention.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜