如何在添加和删除类时触发函数

时间:2016-08-05 11:35:28

标签: javascript jquery html5 addclass

我想触发一个函数,每次我打开打开菜单的类时,都会按顺序为nav li添加动画类

//this is my script

$(document).ready(function() {
document.querySelector("#nav-toggle") 
.addEventListener("click", function() {
this.classList.toggle("active");
$(".menu-wrapper").toggleClass("show-menu");
});  // menu toggle class function
}); //add class for menu

var addClassToEl = function($el) {
$el.addClass('nav-reveals'); // addclass sequentially to nav elements
};

$('.side-menu li').each(function(i, el) {
setTimeout(function() {addClassToEl($(el))}, i++ * 200);
//set delay for each elements
});

1 个答案:

答案 0 :(得分:0)

点击此链接http://jsbin.com/foqajanoya/edit?html,js,output

$('button').on('click',function(e) {
  //do something
  var $inp=$("input");
  var $val= $("input").val();
  $inp.attr('class',$val);
  if($inp.hasClass('active')){
    alert(' is active: Show Menu');
  }
});


<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-2.2.4.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
  <input class="sac"/><button>OK</button>
</body>
</html>