繁体   English   中英

如何删除同一页面上的不同项目

[英]How can i remove different items on same page

我如何在不重新加载页面的情况下删除同一页面上的不同项目,就像我想删除一些项目并留下 rest

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("p").remove();
  });
});
</script>
</head>
<body>

<div>
<p>remove this paragraph</p><button>close</button>
<p>leave this paragraph</p><button>close</button>
<p>remove this paragraph</p><button>close</button>
<p>leave this paragraph</p><button>close</button>
</div>
</body>
</html>

这里有一种方法可以删除按钮和按钮的上一段:

$(document).ready(function(){
  $('button').click(function(){
    $( this ).prev('p').remove();
    $( this ).remove();
  });
});

在那里演示: https://codepen.io/aureliendebord/full/vYNqdrw

好吧,您需要更改 html 以定位要为其添加自定义class的项目,例如:

<p>remove this paragraph</p><button>close</button>
<p>leave this paragraph</p><button>close</button>
<p class="item-to-remove">remove this paragraph</p><button>close</button>
<p>leave this paragraph</p><button>close</button>

因此您还需要更改 javascript 中的 CSS 选择器:

$(document).ready(function(){
  $("button").click(function(){
    $(".item-to-remove").remove();
  });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM