如何通过jQuery

时间:2017-04-07 05:56:43

标签: jquery html

我有一个按钮,我希望它在点击时创建或追加元素,1秒后它必须自动删除 我该怎么做

3 个答案:

答案 0 :(得分:1)

你可以试试这个

  $('#btnId').click(function(){
 $( "#divId" ).append( "<p id="test">Test</p>" );

     setTimeout(function(){
      $('#test').remove();
    }, 1000);
    })

答案 1 :(得分:0)

&#13;
&#13;
$(document).ready(function(){
  $(".abc").click(function(){
        $( ".inner" ).append( "<p>Test</p>" );
        setTimeout(function(){$('p').remove();}, 1000);
      });
 });
&#13;
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
</head>
<body>

 <div class="inner">
 <input type="button" value="Click me" class="abc">
 </div>
</body>
</html>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

您可以使用jQuery .append()setTimeout

   $('#btnId').click(function(){
     $( "#divToAppendId").append( "<span id="tempDivId">Element</span>" );

     setTimeout(function(){$('#tempDivId').remove();}, 1000);
  })

这里1000是以毫秒为单位的单位,表示1秒。因此,1秒元素将被删除。