2012-09-06 38 views
-1

您好我想在jQuery中刪除div。這是我的代碼,但我有測試,並且不起作用,div不會刪除。幫我。jQuery刪除()從div不起作用

var img_old=$(".colCenter").find('#div_'+old_id); 
img_old.delay(10).animate({top:1440},1500, function(){ 
img_old.remove(); 
}); 

這是HTML

<div class="colCenter" id="two"> 
    <div id="randomdiv" style="display:none;"></div> 
    <div class="content" id="div_index"> 
     <div class="img_background"> 
         <img src="img/sfondi/index.jpg" alt="" class="old_img" id="img_index"/> 
        </div> 
        <div class="logo_home"> 
         <a href="index.php"><img src="img/logo_grande.png" alt="Web & Design" title="Web & Design" /></a> 
        </div> 
       </div> 
</div> 
+1

所以做'img_old'居然有一個元素的引用? – alex

+2

你可以發佈你的HTML請。 – Lowkase

+1

和http://jsfiddle.net/演示。 –

回答

2

http://jsfiddle.net/mblase75/FJchB/ - 你的代碼工作正常,我。

也許變量old_id未在您的代碼中設置?我不得不將它設置爲「索引」以獲得任何發生。

正如其他人所建議的那樣,您還需要將代碼包裝在$(document).ready{...}塊中,以便它僅在元素加載後運行。

+0

設置爲beacuse是一個動態頁面 –

1
<script> 
$(function(){ 
var img_old=$(".colCenter").find('#divx'); 
img_old.delay(10).animate({top:1440},1500, function(){ 
img_old.remove(); 
}); 
}); 
</script> 
<div class="colCenter"> 
<div id="divx">Another one text 3</div> 
</div> 

適合我。 嘗試執行DOM代碼準備好的地方。

0

在這個例子在這裏做工精細:

jsfiddle

HTML

ID: <input type="text" id="id" value="0"/> 

<div> 
    <div class="colCenter"> 
     <div id="div_0">0</div> 
     <div id="div_1">1</div> 
     <div id="div_2">2</div> 
     <div id="div_3">3</div> 
    </div> 
</div> 

<input type="button" id="button" value="Go"/> 

JS

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

     var old_id = $('#id').val(); 
     var img_old = $(".colCenter").find('#div_' + old_id); 
     img_old.delay(10).animate({ 
      top: 1440 
     }, 1500, function() { 
      img_old.remove(); 
     }); 
    }); 
});​ 
1

檢查old_id是「索引」,否則你的代碼不起作用。因爲只有div_index。

嘗試做之間的這種代碼:

$('document').ready(function() { 

} 
0

試試這個

var old_id = 'index'; 
var img_old = $('.colCenter').find('#div_' + old_id); 
img_old.delay(10).animate({ 
    top: 1440 
}, 1500, function() { 
    img_old.remove(); 
});