jQuery 不能由 if 语句触发

jquery can't be triggered by if statement

本文关键字:语句 if 不能 jQuery      更新时间:2023-09-26

我尝试在每个.noti-button的父div消失后#no-noti出现,使用if语句但没有运气。代码有什么问题吗?

$(document).ready(function(){
  $(".noti-button").on("click", function(){
    $(this).parent().fadeOut("slow")
  });
  if( $(".noti-box-pt").css("display") === "none" ){
      $("#no-noti").fadeIn("slow");
  }
});

我想你想使用fadeOut的回调。淡出完成后,它将触发。

$(document).ready(function(){
  $(".noti-button").on("click", function(){
    $(this).parent().fadeOut("slow", function() {
       $("#no-noti").fadeIn("slow");
    });
  });
});

编辑 此外,请考虑将if语句更改为:

if( $(".noti-box-pt").is(':hidden') ) {
    ...
}

您需要从点击处理程序中触发fadeIn() fadeOut()语句。

http://jsfiddle.net/tnjpe1cw/

$(document).ready(function(){
  $(".noti-button").on("click", function(){
    $("#no-noti").fadeIn("slow");
  });
});

这使您可以控制动画。目前,if 语句只会在页面加载时运行一次,而不会在单击按钮时运行一次。

希望这有帮助!