开发者

Remove a DIV using a link

HTML:

<div class="row">
<!-- content -->
<a href="#" class="remove">remove</a>
</div>

JS:

$('.remove').click(function(){
  $(this).parent().remove();
  return false;
});

If there is only one DIV it works OK but when there are multiple DIVS (class="row") it won't do anything. No errors are returned either.

EDIT: I should add that by default there is only one div - additional divs 开发者_如何学编程get dynamically created using a jQuery clone function - perhaps this is why it won't recognise the new link/div?


It works for me: JS Fiddle demo.

Given that the other divs are dynamically loaded, you need to use:

$('.remove').live('click',
  function(){
      $(this).parent().remove();
      return false;
});

JS Fiddle demo (featuring add and remove).


If I am understanding you correctly - it seems to be working as intended - Demo


Make sure your code is only run once the whole page has loaded, i.e.:

$(document).ready(function() {
  $('.remove').click(function(){
    $(this).parent().remove();
    return false;
  });
});


Seems to work just fine for me. Check out my JSFiddle (with added removal animation for fun)

UPDATE

each time you create a new items, you need to re-execute the code

doCloneObject();

//Once you create the new object(s), tell jQuery what to do with them.
$('.remove').click(function(){
    $(this).parent().toggle('fast', function (){
            $(this).remove();
    });
    return (false);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜