jQuery 检查是否有 class 并添加 css

jQuery check if has class and add css

我正在编写一个 jquery 脚本来检查模态框是否弹出并将 css 添加到 class 以便固定导航栏不会阻止调用的模态框。

$(document).ready( function() {
 if ($('#myModal').hasClass('in'))  {
     $(".cbp-af-header").css("z-index", "0 !important");  
 } 
  else { 
     $(".cbp-af-header").css("z-index", "10000 !important");  
  }

});

好像不行。有帮助吗?

如果它是 bootstrap 模态,最好使用它的模态显示和隐藏事件

$("#myModal").on("shown.bs.modal", function(){
   $(".cbp-af-header").css("z-index", "0 !important");
});
$("#myModal").on("hidden.bs.modal", function(){
   $(".cbp-af-header").css("z-index", "10000 !important");
});