如何使用 php 触发元素隐藏

How to trigger an element to hide using php

我有以下 php 代码可以回应警报。

echo '<div class="alert alert-warning alert-dismissible" id = "noOperator" role="alert" >
      <strong>Not certified! </strong>'.$checkBoxValue.' is not certified to use '.$needleType.'
      <button type="button" class="close" data-dismiss="alert" aria-label="Close">
      <span aria-hidden="true">&times;</span></button>
      </div>';

但我想在显示后关闭此警报。因此,我再次给出了以下回声。

echo ' <script>
      $("#noOperator").fadeTo(2000, 500).slideUp(500, function(){
           $("#noOperator").slideUp(500);
      });
       </script>';

但是没用

有人知道为什么吗?

为什么不使用 JQuery 而不回显使用 PHP 的代码。并检测元素是否可见以触发隐藏功能。或者只使用警告框的关闭按钮。

setTimeout(function() {
  if ($("#noOperator").is(":visible")) {
    //$('#noOperator').hide();
     $("#noOperator").animate({
        'margin-top' : "-50%",
        'opacity' : '0',
        'visibility' : 'hide'
      },1000);
   
    console.log("hiding now")
  }
  
}, 1000); // hide the element if visible after 1 second


//or just use the button on the alert box
//if you want it just uncomment below

//$('.close').click(function(){
//    $('#noOperator').hide();
//});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="alert alert-warning alert-dismissible" id="noOperator" role="alert">
  <strong>Not certified! </strong>
  <button type="button" class="close" data-dismiss="alert" aria-label="Close">
      <span aria-hidden="true">&times;</span></button>
</div>

Your code will work if you change it to:

echo '<script>
         setTimeout(function() {
            $("#noOperator").fadeTo(2000, 500).slideUp(500, function(){
              $("#noOperator").slideUp(500);
            });
         }, 3000);
      </script>';