2012-04-09 65 views
2

如果我有這樣的代碼:替代HTML內容 - 保持一個元素

<div class="lists"> 
    <a href="#new_list">New list</a> 
</div> 

$.ajax({ 
    url: 'xxx.php', 
    type: 'POST', 
    data: { 
     id_user:<?php echo $id;?> 
    }, 
    dataType: 'html', 
    success: function (data) { 
     $(".lists").html(data); //here 
    } 
}); 

有可能忽略.lists>a時更換內容?我的意思是:添加新的內容data還要保持原有的.lists>a

感謝

+0

如果數據是有效的HTML,你可以使用'.append(數據)'或'.prepend(數據)',搜索在文檔中獲取更多信息。 – NicoSantangelo 2012-04-09 17:58:41

回答

2

a備份內lists後來append/prepend他們,

var $a = $(".lists a").clone(); 
$(".lists").html(data).append($a); 

使用.prepend insted的的.append如果你想要的鏈接,是數據的HTML

注意上面:

  1. 假設a標籤s始終位於.lists的頂部/底部。
  2. 你有很多業內.lists等元素要與數據替換
0

試試這個

$(".lists a").html(data); 
1

我相信這是你問什麼?

只需附加html()和數據即可。

乾杯。

$.ajax({ 
    url: 'xxx.php', 
    type: 'POST', 
    data: { 
     id_user:<?php echo $id;?> 
    }, 
    dataType: 'html', 
    success: function (data) { 
     $(".lists").html($(".lists").html() + data); //here 
    } 
}); 
0

改爲使用.append()。在jQuery append文檔中引用一個gander。