javascript remove all elements :Uncaught SyntaxError: Unexpe

javascript remove all elements :Uncaught SyntaxError: Unexpected identifier

本文关键字:Uncaught Unexpe SyntaxError elements remove all javascript      更新时间:2023-09-26

这类问题在这个网站上被问了很多,但我不明白为什么会发生这种情况。我想写一个脚本,删除与类"not_imp_msg"的所有元素,我做了这个

setTimeout($('.not_imp_msg').each(function(){$(this).slideDown();}),2000); 

在laravel和我的flash。blade。php

中是用于flash消息的
@foreach($flash as $flash_message)
    @if(isset($flash_message['status']) && $flash_message['status'] != null)
<div class="box box-{{$flash_message['class']}} box-solid">
    <div class="box-tools pull-right">
      <button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
    </div>
    <div class="box-body">
      {{$flash_message['message']}}
    </div>
  </div>
    @else
    <div class="box box-{{$flash_message['class']}} box-solid not_imp_msg">
      <div class="box-body">
      {{$flash_message['message']}}
    </div>
  </div>
    @endif
@endforeach

这让我在2秒后出现错误,但所有元素在文档加载后立即滑动。它不应该等待2秒。我不明白为什么。

尝试传递匿名函数,因为setTimeout正在等待函数名或匿名函数:

$(document).ready(function() {
  setTimeout(function() {
    $('.not_imp_msg').slideDown();
  }, 2000);
});
.not_imp_msg {
  background-color: #ddd;
  border-top: 1px solid white;
  height: 50px;
  display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="not_imp_msg"></div>
<div class="not_imp_msg"></div>
<div class="not_imp_msg"></div>
<div class="not_imp_msg"></div>
<div class="not_imp_msg"></div>


如果要删除元素,则使用slideUp(callback).remove元素的回调函数。


.slideDown将显示元素,.slideUp将折叠元素,但它们仍将以0px高度显示在DOM中

试试:

setTimeout(
function () {
    $('.not_imp_msg').each(function(){
       $(this).slideDown();
    };
}, 2000); 

希望它工作!顺便问一下,您能在控制台中提供错误消息吗?

你只是忘了把setTimeout的内部包装在一个函数中

setTimeout(function(){
    $('.not_imp_msg').each(function(){$(this).slideDown();}) 
},2000);