开发者

Conditional check on click

开发者 https://www.devze.com 2023-02-20 23:17 出处:网络
I have an on click function that toggles a class. $(\".main\").click(function() { $(this).toggleClass(\"classA classB\");

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

精彩评论

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

关注公众号