2015-01-16 94 views
0
$(wrapper).on("click",".remove_field", function(e){ //user click on remove text 
    e.preventDefault(); $(this).parent('div').remove();x--; 
})  

以上是我的javascript刪除的div是動態生成的,如的Javascript上刪除DIV

<div class="newRow">Some Text &nbsp;<a href="#" class="remove_field">Remove</a></div> 

但在這裏是因爲我用引導網格列

<div class="row"> 
    <div class="col-xs-5"> 
     <div class="newRow">Some Text</div> 
     <div class="col-xs-5"> 
      <div class="newRow"> 
       Some Text Part 2&nbsp;<a href="#" class="remove_field">Remove</a> 
      </div> 
     </div> 
    </div> 
</div> 

相反的問題刪除'div',我想刪除div class =「row」 ,這樣它刪除整行。我該如何改變我的代碼才能發生。

回答

2

使用.closest()選擇:

$(wrapper).on("click",".remove_field", function(e){ //user click on remove text 
e.preventDefault(); $(this).closest('.row').remove();x--; 
}) 
0

您可以刪除使用.parent()

$(document).on("click",".remove_field", function(e){ //user click on remove text 
 
    $(this).parent().parent().parent().parent().remove(); 
 
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<div class="row"> 
 
    <div class="col-xs-5"> 
 
     <div class="newRow">Some Text</div> 
 
     <div class="col-xs-5"> 
 
      <div class="newRow"> 
 
       Some Text Part 2&nbsp;<a href="#" class="remove_field">Remove</a> 
 
      </div> 
 
     </div> 
 
    </div> 
 
</div>