简体   繁体   English

单击时删除父元素

[英]Delete parent element on click

I'm trying to delete the li when I click the button with a span but so far no success. 当我点击带有跨度的按钮时,我试图删除li,但到目前为止还没有成功。

 //delete testCase row when click delete button $("a[id^='delete-']").click(function() { console.log('clicked'); $(this).remove(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> <ul id="testCaseList" class="list-group"> <li class="list-group-item justify-content-between">test1<a class="action-icon" id="delete-test1" name="test1"><span class="fa fa-trash"></span></a></li> <li class="list-group-item justify-content-between">test2<a class="action-icon" id="delete-test2" name="test2"><span class="fa fa-trash"></span></a></li> </ul> 

This issue is because you delete this only, ie. 这个问题是因为你只删除this ,即。 the a . a You need to traverse the DOM to find the parent li , then remove that: 您需要遍历DOM以查找父li ,然后删除:

 $("a[id^='delete-']").click(function() { console.log('clicked'); $(this).closest('li').remove(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> <ul id="testCaseList" class="list-group"> <li class="list-group-item justify-content-between">test1<a class="action-icon" id="delete-test1" name="test1"><span class="fa fa-trash"></span></a></li> <li class="list-group-item justify-content-between">test2<a class="action-icon" id="delete-test2" name="test2"><span class="fa fa-trash"></span></a></li> </ul> 

You have to delete the parent. 您必须删除父级。 Not just itself. 不只是自己。

 //delete testCase row when click delete button $("a[id^='delete-']").click(function() { console.log('clicked'); $(this).parent().remove(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> <ul id="testCaseList" class="list-group"> <li class="list-group-item justify-content-between">test1<a class="action-icon" id="delete-test1" name="test1"><span class="fa fa-trash"></span></a></li> <li class="list-group-item justify-content-between">test2<a class="action-icon" id="delete-test2" name="test2"><span class="fa fa-trash"></span></a></li> </ul> 

You are currently targeting the span not it's parent li which causing the deletion of span . 您目前的目标是span而不是它的父li导致删除span Target the parent of clicked span by changing $(this).remove(); 通过更改$(this).remove();定位单击span的父级$(this).remove(); to $(this).parent().remove(); to $(this).parent().remove(); .

 //delete testCase row when click delete button $("a[id^='delete-']").click(function() { console.log('clicked'); $(this).parent().remove(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> <ul id="testCaseList" class="list-group"> <li class="list-group-item justify-content-between">test1<a class="action-icon" id="delete-test1" name="test1"><span class="fa fa-trash"></span></a></li> <li class="list-group-item justify-content-between">test2<a class="action-icon" id="delete-test2" name="test2"><span class="fa fa-trash"></span></a></li> </ul> 

If you want to remove the li you can use jQuery closest to get to the outer li and remove that. 如果你想删除li ,你可以使用jQuery 最近去外li并删除。

$(this).closest('li').remove();

 //delete testCase row when click delete button $("a[id^='delete-']").click(function() { console.log('clicked'); $(this).closest('li').remove(); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous"> <ul id="testCaseList" class="list-group"> <li class="list-group-item justify-content-between">test1<a class="action-icon" id="delete-test1" name="test1"><span class="fa fa-trash"></span></a></li> <li class="list-group-item justify-content-between">test2<a class="action-icon" id="delete-test2" name="test2"><span class="fa fa-trash"></span></a></li> </ul> 

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

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