如何不断检查上课的div

当您单击该区域以检查它是否具有“top-nav”时,它会工作一次,但是如何使其不断检查以便每次警报都没有工作,或者具有“top-nav”类


jsfiddle:https://jsfiddle.net/jzhang172/117mg0y1/


 if(!$('.view-projects').hasClass("top-nav")){

 alert('has');


 }else if($('.view-projects').hasClass("top-nav")){

  alert('hey');

   

 }

 

 $('.push').click(function(){

 $('.view-projects').toggleClass('top-nav');

 });

.view-projects{

  height:100px;

  width:100%;

  background:black;

}

body,html{

  margin:0;

  padding:0;

}

.push{

  height:200px;

  width:100px;

  background:blue;

}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="view-projects"></div>


<div class="push"></div>


交互式爱情
浏览 383回答 4
4回答

米琪卡哇伊

当您单击“.push”元素时,您似乎想做两件事:用“top-nav”切换“.view-projects”类并...如果“.view-projects”有或没有此类,则发出警报。也就是说,没有必要经常检查间隔。由于这两种情况都取决于点击处理程序...使用点击处理程序:)$('.push').click(function(){ &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;First&nbsp;toggle&nbsp;the&nbsp;class &nbsp;&nbsp;&nbsp;&nbsp;$('.view-projects').toggleClass('top-nav'); &nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Now&nbsp;do&nbsp;the&nbsp;alert&nbsp;based&nbsp;on&nbsp;what&nbsp;the&nbsp;current&nbsp;class&nbsp;is &nbsp;&nbsp;&nbsp;&nbsp;var&nbsp;hasTopNav&nbsp;=&nbsp;$('.view-projects').hasClass('top-nav');&nbsp;//&nbsp;returns&nbsp;true&nbsp;or&nbsp;false &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(&nbsp;hasTopNav&nbsp;)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("has"); &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alert("hey"); &nbsp;&nbsp;&nbsp;&nbsp;}});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript