【问题标题】:jquery show() function delaysjquery show() 函数延迟
【发布时间】:2017-04-16 08:55:52
【问题描述】:

我使用jquery.show() 来显示在css 中为"display:none" 的div。 但是,show() 函数延迟显示 div。

function button_click(){
    $("#mydiv").show();
    function1();
    function2();
}

我尝试过$('#mydiv').show({queue:false}),但它不起作用。 #mydiv 仅在 function1 和 function2 完成时显示。 如何让show() 立即生效?

更新:我发现这是因为我使用了 $.ajxaSetup({async:false})。
我需要发出多个请求,例如 $.get 并且在所有请求完成后我会做一些事情,所以我将其设为异步...但是这样 $.show() 函数在所有请求完成之前都无法工作...

【问题讨论】:

  • 那么function1和function2是做什么的?
  • .show('fast') 试试这个。

标签: jquery queue show


【解决方案1】:

我不确定您的实际文件,但根据您的代码,这是我使用简单回调的解决方案,函数 one 和 two 在被 main 函数调用之前不会运行,该函数是hide 方法的回调:

function button_click(){
	function one() { console.log('I am one');}
	function two() { console.log('I am two');}
	
	$("#mydiv1").hide('slow', function main(){
		one();
		two();
	 });
}


button_click();
.child {
	color           : white;
	display         : inline-block;
	margin          : 20px;
			
	min-width       : 50px;
	min-height      : 50px;
	padding         : 10px;
	background-color: olive;
  }
		
.parent {
	padding         : 20px;
	background-color: beige;
	}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="father" class="parent">
			  <div id="mydiv1" class="child">1</div>
			  <div id="mydiv2" class="child">2</div>
			  <div id="mydiv3" class="child">3</div>
			  <div id="mydiv4" class="child">4</div>
			  <div id="mydiv5" class="child">5</div>
			</div>

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 2013-02-07
    • 2011-06-24
    相关资源
    最近更新 更多