当多个w.r.到父div时,删除子div

时间:2018-05-23 04:40:28

标签: javascript jquery html css twitter-bootstrap

我有两个动态创建的div:



<div id="starredDiv">
  <div class="list-group" id="starredList">
    <div class="list-group-item" style="border-left: none; border-right: none;">
      <img class="a-img" src="./img/desktop.png" height="25" width="25">
      <a class="a-file">message.txt</a>
      <button class="btn glyphicon glyphicon-ok btn-sm btn-current btn-default" style="float: right;"></button>
      <button class="btn glyphicon glyphicon-star btn-sm btn-star btn-primary" style="float: right;"></button>
    </div>
    <div class="list-group-item" style="border-left: none; border-right: none;">
      <img class="a-img" src="./img/desktop.png" height="25" width="25">
      <a class="a-file">testcase.txt</a>
      <button class="btn glyphicon glyphicon-ok btn-sm btn-current btn-default" style="float: right;"></button>
      <button class="btn glyphicon glyphicon-star btn-sm btn-star btn-primary" style="float: right;"></button>
    </div>
  </div>
</div>


<div id="recentDiv">
  <div class="list-group" id="recentList">
    <div class="list-group-item" style="border-left: none; border-right: none;">
      <img class="a-img" src="./img/desktop.png" height="25" width="25">
      <a class="a-file">message.txt</a>
      <button class="btn glyphicon glyphicon-ok btn-sm btn-current btn-default" style="float: right;"></button>
      <button class="btn glyphicon glyphicon-star btn-sm btn-star btn-primary" style="float: right;"></button>
    </div>
    <div class="list-group-item" style="border-left: none; border-right: none;">
      <img class="a-img" src="./img/desktop.png" height="25" width="25">
      <a class="a-file">testcase.txt</a>
      <button class="btn glyphicon glyphicon-ok btn-sm btn-current btn-default" style="float: right;"></button>
      <button class="btn glyphicon glyphicon-star btn-sm btn-star btn-primary" style="float: right;"></button>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

当用户点击recentList的glyphicon-star按钮时,我想删除starredList中存在的list-group-item w.r.to <a>。为此,我写了一些这样的事情:

var file = $(this).closest('.list-group-item').children('.a-file').text();

  if($(#starredList).children('.list-group-item').children('.a-file').text() == file){
    $(#starredList).children('.list-group-item').remove();
  }

当starredList与一个list-group-item一起出现但不在多个上时,这是有效的吗?

2 个答案:

答案 0 :(得分:2)

.list-group-item是多个,所以你需要在它上面循环,

$('#starredList').children('.list-group-item').each(function(){
  if($(this).children('.a-file').text() == file){
     $(this).remove();
  }
});

答案 1 :(得分:0)

这应该这样做。

var file = $(this).closest('.list-group-item').children('.a-file').text();

$('.list-group-item').not($(this).closest('.list-group-item')).each(function(){
   if($(this).find('.a-file:contains('+file+')').length > 0){
       $(this).remove();
   }
});