如何在jQuery中延迟insertBefore() [英] how to delay insertBefore() in jquery

查看:169
本文介绍了如何在jQuery中延迟insertBefore()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用insertBefore()函数2次,所以我需要延迟第二次.我有什么办法吗?

I need to use insertBefore() function 2 times, so i need to delay the second one. Is there any way I can do it?

<button>Click</button>

<br><br>
<p id="p1">This is a paragraph1.</p>
<p id="p2">This is a paragraph2.</p>
<p id="p3">This is a paragraph3.</p>

</body>

<script>
$(document).ready(function(){

    $("button").click(function(){

             $("#p3").insertBefore("#p2");
             $("#p3").delay(1000000).insertBefore("#p1"); 
        });});
</script>

推荐答案

您可以使用setTimeout();

$(document).ready(function(){
    $("button").click(function(){
       $("#p3").insertBefore("#p2");
       setTimeout(function(){
         $("#p3").insertBefore("#p1"); 
         }, 1000);
    });
});

演示

这篇关于如何在jQuery中延迟insertBefore()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆