使用 jquery 在 X 秒后隐藏/删除标签

发布于 2024-08-02 09:42:27 字数 68 浏览 2 评论 0原文

我想知道如何在一段时间后隐藏/删除标签。是否有一些内置的东西或者我是否使用线程(如果javascript可以做到这一点?)

I am wondering how can I hide/remove a tag after a certain amount of time. Is there some built in thing or do I do use threading(if javascript can do this?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怪我鬧 2024-08-09 09:42:27

您甚至不需要 jQuery 来实现“5 秒”部分:JavaScript 的内置 setTimeout 函数即可实现这一目的。结合 jQuery 进行 DOM 操作,您将得到:

setTimeout(function() {
  $("#the-tag-you-want-to-remove").remove();
}, 5000);

这里 5000 代表 5000 毫秒或 5 秒。您可以传递 setTimeout 现有函数或(如本例中)匿名函数。

You don't even need jQuery for the "5 seconds" part: JavaScript's built-in setTimeout function will do the trick. Incorporating jQuery for the DOM manipulation, you get:

setTimeout(function() {
  $("#the-tag-you-want-to-remove").remove();
}, 5000);

Here the 5000 represents 5000 milliseconds, or 5 seconds. You can pass setTimeout an existing function or (as in this case) an anonymous function.

甲如呢乙后呢 2024-08-09 09:42:27

尝试使用 .delay() 函数

http://api.jquery.com/delay/

Try to use .delay() function

http://api.jquery.com/delay/

孤凫 2024-08-09 09:42:27
window.setTimeout( hideTagFn, 5000);

function hideTagFn(){

   $('#someElementId').hide();
}
window.setTimeout( hideTagFn, 5000);

function hideTagFn(){

   $('#someElementId').hide();
}
他是夢罘是命 2024-08-09 09:42:27

这几乎与上面的答案类似,但在这个示例中,您只需按原样复制并将其粘贴到编辑器中即可工作。

<hmtl>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script>
$(document).ready(function(){
  $('.showupAfter8seconds').hide();
  setTimeout(function(){
    $('.showupAfter8seconds').show();
  },8000);
});
    </script>
  </head>
  <body>
    <div class="showupAfter8seconds">
      <h1>I was hidden for 8 seconds</h1>
    </div>

  </body>
</html>

This is almost similar to the above answers but in this example you just have to copy as it is and paste it in your editor then it will work.

<hmtl>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
    <script>
$(document).ready(function(){
  $('.showupAfter8seconds').hide();
  setTimeout(function(){
    $('.showupAfter8seconds').show();
  },8000);
});
    </script>
  </head>
  <body>
    <div class="showupAfter8seconds">
      <h1>I was hidden for 8 seconds</h1>
    </div>

  </body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文