javascript - Use the switch statement for classes -
i new switch statement of javascript.
i have big list of list items. each item has own class. each list item else should done.
i create switch code:
$('.nav-main li').click(function() { var item = this; switch (item) { case '.menu-intro': alert("test"); break; case '.menu-intro-second': alert("test2"); break; } }); but problem is: how can check on class names? when nav-main li item hass class 'menu-intro'. must happen. when li item has class 'menu-intro-second'. thing must happen.
how can create this?
thanks!
use this:
$('.nav-main li').click(function() { var item = this; switch ($(item).attr('class')) { // in case the class of element menu-intro case 'menu-intro': alert("test"); break; // in case the class of element menu-intro-second // or in case class menu-intro-third case 'menu-intro-second': case 'menu-intro-third': alert("test3"); break; // in case the classes of element menu-intro , active case 'menu-intro active': alert("test4"); break; // in other cases... default: alert("default"); break; } }); javascript switch-statement
No comments:
Post a Comment