开发者

Conditional check on click

I have an on click function that toggles a class.

$(".main").click(function() {
    $(this).toggleClass("classA classB");
    $(".secondary").toggleClass("classC classD");
));

I need to add开发者_JAVA技巧 a check which class is triggered and add an action based on it, something like.

if ( class == 'classA') {
  // do A
} else {
  // do B
}

Can it be done with jQuery?


You can use .hasClass()

$(".main").click(function() {
  $(this).toggleClass("classA classB");
  $(".secondary").toggleClass("classC classD");

  if ( $(this).hasClass('classA')) {
    // do A
  } else {
    // do B
  }
));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜