2014-04-23 27 views
0

我有以下Jquery代碼。我想要做的是我想清空am-container,但我無法這樣做。無法使用Jquery清空div

jQuery(document).find("div[id='am-container']").html(''); 

以下是我的HTML結構。

<div class="mid-part-right"> 
<div class="match-image"> 
    <div class="main-gallary"> 
    <div class="container"> 
    <div id="am-container" class="am-container"> 
    <a class="draggable am-wrapper" href="#" style="width: 233px; height: 133px; margin: 2px;"> 
    <a class="draggable am-wrapper" href="#" style="width: 233px; height: 133px; margin: 2px;"> 
<a class="draggable am-wrapper" href="#" style="width: 263px; height: 133px; margin: 2px;"> 
    </div> 
    </div> 
    </div> 
</div> 
</div> 
+3

爲什麼不簡單'$('#am-container')。html('');'? –

+1

'$('#am-container')。empty();' – Stefan

+0

您有重複的ID嗎?如果不這樣做,ID必須是唯一的。然後,你的代碼將大部分工作。在這裏工作很好:http://jsfiddle.net/urqAu/1/ – Anton

回答

2

它:

$("#am-container").children().text(""); 


$("div#am-container a").each(function(el){$(el).empty();}); 
3

嘗試:

$('#am-container').html(''); 

或者

$('#am-container').empty(); 

如果你想刪除容器和它的孩子,使用:

$('#am-container').remove(); 
+0

嗯,我試過這個,它不工作 –

+0

工程很好http://jsfiddle.net/j08691/6YMpb/。只要你關閉你的錨標籤。 – j08691

3
jQuery('#am-container').html(''); 

試試這個:

$('#am-container').remove(); 
+0

嗯,我試過這個,它不工作 –

+0

@ noobie-php:你有輸出。請檢查最後更新的行。 – Rubyist

2

你可以簡單地像

$("#am-container").empty(); 
2

既然你要刪除你要選擇元素使用聽起來像你想刪除元素am-container不是它包含的內容,試試這個

$('#am-container').remove(); 
+0

那麼你的答案几乎是相同的,但我標記正確的1是我列表中第一個顯示 –