简体   繁体   English

Jquery附加元素混乱

[英]Jquery appending element confusion

i have the following javascript: 我有以下javascript:

    $('#span_search').hide();

    $('#team_header').append($('<a href="#group-form" id="btn_new_category" class="btn btn-s-md btn-info" style="float: right;" data-toggle="modal">New Category</a>').hide().delay(500).fadeIn());

    $('#span_search').delay(1500).show();

Now i would assume that the span element would be visible later than the appended item but this is not the case... Why? 现在我假设span元素晚于附加项目可见,但事实并非如此......为什么?

delay works with only with functions that uses a queue for execution, where the execution of the next item in the queue is delayed till the timer is ran out. delay仅适用于使用队列执行的函数,其中队列中下一项的执行被延迟,直到计时器用完为止。

.show() (without any argument) doesn't use a queue based execution, but methods that uses animation like fadeIn() , slideToggle() etc does. .show() (没有任何参数)不使用基于队列的执行,但使用fadeIn()slideToggle()等动画的方法确实如此。

One solution is to queue a function yourself which will display the content as shown below 一种解决方案是自己排队函数,该函数将显示如下所示的内容

$('#span_search').delay(1500).queue(function(){
    $(this).show()
})

Demo: Fiddle 演示: 小提琴

or use a animated version 或使用动画版本

$('#span_search').delay(1500).fadeIn();
$('#span_search').delay(1500).slideToggle();
$('#span_search').delay(1500).show(1);

Demo: Fiddle 演示: 小提琴

It says in the jQuery documentation: 它在jQuery文档中说:

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

... this will not delay the no-arguments forms of .show() or .hide() which do not use the effects queue. ...这不会延迟不使用效果队列的.show()或.hide()的无参数形式。

One big misunderstanding is thinking .delay will delay the functions chained after .delay . 一个很大的误解就是思考.delay会延迟在.delay之后链接的功能。 That is not true because .delay is used to delay the animation queue only. 这不是真的,因为.delay仅用于延迟动画队列。

To add a function into the animation queue, you can use .queue : 要将函数添加到动画队列中,可以使用.queue

$('#span_search').hide();
$('#team_header').append($('<a href="#group-form" id="btn_new_category" class="btn btn-s-md btn-info" style="float: right;" data-toggle="modal">New Category</a>').hide().delay(500).fadeIn());
$('#span_search').delay(1500).queue(function(){
    $(this).show();
})

or set the interval to zero: 或将间隔设置为零:

$('#span_search').delay(1500).show(0);

http://jsfiddle.net/DerekL/egdtD/ http://jsfiddle.net/DerekL/egdtD/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM