您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用jQuery在特定时间间隔显示和隐藏div

使用jQuery在特定时间间隔显示和隐藏div

此处的 -在URL中添加 以与代码一起播放您只需要使用JavaScriptsetInterval)函数

$('html').addClass('js');



$(function() {



  var timer = setInterval(showDiv, 5000);



  var counter = 0;



  function showDiv() {

    if (counter == 0) {

      counter++;

      return;

    }



    $('div', '#container')

      .stop()

      .hide()

      .filter(function() {

        return this.id.match('div' + counter);

      })

      .show('fast');

    counter == 3 ? counter = 0 : counter++;



  }



});


<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"

       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">



<head>

  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

  <title>Sand@R_388_2419@</title>

  <Meta http-equiv="Content-type" content="text/html; charset=utf-8" />

  <style type="text/css" media="screen">

    body {

      background-color: #fff;

      font: 16px Helvetica, Arial;

      color: #000;

    }



    .display {

      width: 300px;

      height: 200px;

      border: 2px solid #000;

    }



    .js .display {

      display: none;

    }

  </style>

</head>



<body>

  <h2>Example of using setInterval to trigger display of Div</h2>

  <p>The first div will display after 10 seconds...</p>

  <div id='container'>

    <div id='div1' class='display' style="background-color: red;">

      div1

    </div>

    <div id='div2' class='display' style="background-color: green;">

      div2

    </div>

    <div id='div3' class='display' style="background-color: blue;">

      div3

    </div>

    <div>

</body>



</html>

为了回应您对容器div的评论,只需对此进行修改

$('div','#container')

对此

$('#div1, #div2, #div3')
JS 2022/1/1 18:17:37 有387人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶