Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery:隐藏和显示div';s_Jquery - Fatal编程技术网

jQuery:隐藏和显示div';s

jQuery:隐藏和显示div';s,jquery,Jquery,我想让div看3秒钟。并显示下一个div,持续3秒。然后继续计时。 但如果有其他代码来解决此问题,则此代码不起作用 var timer = setInterval(showDiv(3000)); var counter = 0; function showDiv() { if (counter ==0) counter++; $('#text1, #text2').stop().hide().filter(function() { return this.id.ma

我想让div看3秒钟。并显示下一个div,持续3秒。然后继续计时。 但如果有其他代码来解决此问题,则此代码不起作用

var timer = setInterval(showDiv(3000));
var counter = 0;

function showDiv() {
  if (counter ==0) 
    counter++;
  $('#text1, #text2').stop().hide().filter(function() { 
    return this.id.match('#text' + counter); 
  }).show();
  counter == 2? counter = 0 : counter++; 
}
showDiv();

});

如果元素匹配或不匹配,filter函数应返回布尔值。这里this.id.match返回一个数组,该数组是正则表达式执行的结果。您可以将其简化为以下方式,而不是使用函数进行筛选:

  setInterval(showDiv,3000);
  var counter = 0;
  function showDiv() {
    if (counter ==0) { counter++;}
    $('#text1, #text2')
      .stop()
      .hide()
      .filter('#text'+counter)   
      .show();
    counter == 2? counter = 0 : counter++; 
  }

它应该是
setInterval(showDiv,3000)我认为您可以使用jqueryanimate,它更适合。