.parent().remove() issue

非 Y 不嫁゛ 提交于 2019-11-30 11:23:13

Try:

$(document).on('click', '.remove', function() {
    $(this).parent().remove();
});

Events are bound on page load so newly added element aren't.

The 'live' function call has been deprecated and no longer works. You can find instructions for how to rewrite functions using the replacement 'on()' method here and more info about the deprecation:

http://api.jquery.com/live/

To handle all current and newly created elements, you must now use:

$(document).on("click", ".remove", function() {
    $(this).parent().remove();
});

jQuery runs on the document object, not the element and there is an additional parameter to specify which elements to watch and add event listeners to.

Make sure you close your expresion:

$(".remove").click(function() {
    $(this).parent().remove();
});

You can do this for delete the parent:

 $(document).on("click", ".remove", function() { 
      $(this).parent().remove(); 
 });

or you can do this for delete all parents:

 $(document).on("click", ".remove", function() { 
      $(this).parents().remove();
 });
davin

The elements don't originally exist, which means you need to use .live() or .delegate()

For example, change:

use $(".remove").click(...) instead of $(".remove").live("click", ...) this

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!