簡體   English   中英

使用jQuery將相同的html多次插入元素

[英]Insert same html into element multiple times with jQuery

我希望用戶單擊按鈕,然后將一些html彈出到元素中。 我這樣做了,但是可以用,但是只能用一次。 如果他們再次單擊該按鈕,則什么也不會發生。 我認為使用empty()可以解決問題,但不能解決。 我的代碼有什么問題。

<script type="text/javascript" >

$(document).ready( function() {

   $('#button').on('click', function () {

     $('#ul').html('<li>testing testing</li>').hide(1000, function() {

        $(this).empty();    

     });    

   });

});

</script>

<input type="button" value="click me" id="button" />

<ul id="ul">

</ul>

第二次單擊該按鈕時該元素將隱藏。 你可以做:

$('#ul').show().html('...

http://jsfiddle.net/KXkgZ/

$("#button").click(function(){ 
    $("#ul").append("testing testing");
});

如要在每次單擊按鈕時彈出html,則應使用append

$('#ul').append('<li>testing testing</li>');

附錄

jQuery('<li>testing testing</li>').appendTo('#ul').hide(1000, function() {
    $(this).remove();    
 });    

當您執行.hide()時,將CSS選擇器設置為“ display” =“ NONE”,您需要將其更改為如下所示:

$('#ul').html('<li>testing testing</li>').hide(1000, function() {
  $(this).empty();
  $(this).show();
}); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM