gpt4 book ai didi

jQuery-如何仅针对单个列表项进行删除

转载 作者:行者123 更新时间:2023-12-01 06:10:01 24 4
gpt4 key购买 nike

我有一个待办事项列表,但在按下删除按钮时删除单个行项目时遇到问题。使用我当前的代码,它仅删除实际的删除按钮,而不删除列表项。感谢任何帮助,谢谢。

目标:将鼠标悬停在列表项上,然后按删除图标将其从列表中删除。

$(document).ready(function(){

//Declare variables
var $newItem = $('#newItem');
var $addBtn = $('#addBtn');
var $textField = $('#textField');
var $textAddForm = $('#textAddForm');
var $wrapper = $('#wrapper');
var $list = $('ul');
var $glyph = $('.glyphicon')

//hide the Add form until it's needed and put focus on newItem
$textAddForm.hide();
$newItem.focus();

//hide draggable tooltip on mouseover
$wrapper.mouseover(function() {
$('#draggable').fadeOut(1000);
});


//Make the list draggable
$wrapper.draggable();
$wrapper.resizable();

//Hides the newItem button and adds the text field and add button
$newItem.on('click', function(){
$newItem.hide();
$textAddForm.show();
$textField.focus();
});

//Grabs the submission from Add Item
$textAddForm.on('submit', function(e){
var grabText = $textField.val();
var $listItems = $('#listItems ul');

//disables page from submitting and appends the text to list
e.preventDefault();
$listItems.prepend('<li>' + grabText + '<span class="hidden glyphicon glyphicon-remove-sign"></span></li>');

//After inserting item, hides it and re-enable the New Item button
$newItem.show();
$textAddForm.hide();
$textField.val('');
$newItem.focus();
});


//Toggle complete
$list.on('click', 'li', function(){
var $this = $(this);
var copy = $(this).detach();
var hasComplete = $this.hasClass('complete');

$this.toggleClass('complete');

if (hasComplete === true) {
$this.remove();
copy.prependTo('ul');
}
else {
$this.remove();
copy.appendTo('ul');
}

});

//show Delete button on mouseover and remove if it's pressed
$list.on('mouseenter mouseleave', 'li' , function(){
var $this = $(this);
var $thisitem = $this.html();
console.log($thisitem);
$('.glyphicon', this).toggleClass('hidden');

$glyph.on('click', function(){
$thisitem.remove();
});
});

}); //end
body {
text-align: center;
}

ul {
list-style-type: none;
background: orange;
}

h1, h2, li {
font-family: 'Indie Flower', cursive;
}

p {
font-family: 'Cabin', sans-serif;
font-size: 12px;
}

@import url("//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css");

.glyphicon {
margin-right: 30px;
margin-top: 4px;
float: right;
}

.glyphicon:hover {
color: red;
}

.hidden {
visibility: hidden;
}

#logo {
margin-bottom: 30px;
}

#logo h1 {
margin: 0;
padding-bottom: 0;
}

#logo p {
margin: 0;
}

#wrapper {
border-style: solid;
width: 340px;
overflow: hidden;
margin: auto auto;

}

#newItem {
float: right;
margin-right: 20px;
margin-bottom: 20px;
}

#textField {
float: left;
margin-left: 20px;
}

#listItems {
margin-bottom: 30px;
text-align: left;
font-size: 22px;

}

.complete {
text-decoration: line-through;
}
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="style.css" />
<title>The little to do</title>
<meta carset="utf-8" />
<!-- Summon Fonts & Library-->
<link href='https://fonts.googleapis.com/css?family=Indie+Flower' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Cabin' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css" rel="stylesheet">


</head>
<body>
<div id="draggable">
<p>I'm draggable!</p>
</div>
<div id="wrapper">
<div id="logo">
<h1>Project Bacon</h1>
<p>The Electronic Shopping List</p>
</div><!--end logo-->

<div id="listTitle">
<h2>BUY GROCERIES</h2>
<div id="listItems">
<ul>
</ul>
</div><!--end listItems-->

<form id="textAddForm">
<div>
<input id="textField" type="text" name="entry" placeholder="Add item...">
</div>
<div id="addBtn">
<input type="submit" name="addBtn" value="Add" type="button">
</div>
</form><!--end textAddForm-->

<div id="newItemForm">
<button id="newItem" type="button">New Item</button>
</div><!--end newItemForm-->

</div><!--end listTitle-->




</div><!--end wrapper-->

<!--Summon JS-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>

</html>

最佳答案

如果省略单击处理程序的过滤器,您可以根据单击的元素定义不同的行为。在这种情况下,删除按钮会执行删除操作,单击该行项目将切换已完成状态(将其从列表中划掉)。

给定删除按钮元素的选择器,您可以使用 .closest("li") 来定位并删除父 LI 元素。

$(document).ready(function() {

//Declare variables
var $newItem = $('#newItem');
var $addBtn = $('#addBtn');
var $textField = $('#textField');
var $textAddForm = $('#textAddForm');
var $wrapper = $('#wrapper');
var $list = $('ul');
var $glyph = $('.glyphicon')

//hide the Add form until it's needed and put focus on newItem
$textAddForm.hide();
$newItem.focus();

//hide draggable tooltip on mouseover
$wrapper.mouseover(function() {
$('#draggable').fadeOut(1000);
});


//Make the list draggable
$wrapper.draggable();
$wrapper.resizable();

//Hides the newItem button and adds the text field and add button
$newItem.on('click', function() {
$newItem.hide();
$textAddForm.show();
$textField.focus();
});

//Grabs the submission from Add Item
$textAddForm.on('submit', function(e) {
var grabText = $textField.val();
var $listItems = $('#listItems ul');

//disables page from submitting and appends the text to list
e.preventDefault();
$listItems.prepend('<li>' + grabText + '<span class="hidden glyphicon glyphicon-remove-sign"></span></li>');

//After inserting item, hides it and re-enable the New Item button
$newItem.show();
$textAddForm.hide();
$textField.val('');
$newItem.focus();
});


//Toggle complete
$list.on('click', function(e) {
var $targ = $(e.target);
if ($targ.hasClass("glyphicon")) {
// remove button clicked
$targ.closest("li").remove();
} else if ($targ.is("li")) {
// toggle complete status of line
var copy = $targ.detach();
var hasComplete = $targ.hasClass('complete');
$targ.toggleClass('complete').remove();
if (hasComplete) {
copy.prependTo('ul');
} else {
$targ.remove();
copy.appendTo('ul');
}
}
});

//show Delete button on mouseover and remove if it's pressed
$list.on('mouseenter mouseleave', 'li', function() {
var $this = $(this);
var $thisitem = $this.html();
console.log($thisitem);
$('.glyphicon', this).toggleClass('hidden');

$glyph.on('click', function() {
$thisitem.remove();
});
});

}); //end
body {
text-align: center;
}
ul {
list-style-type: none;
background: orange;
}
h1,
h2,
li {
font-family: 'Indie Flower', cursive;
}
p {
font-family: 'Cabin', sans-serif;
font-size: 12px;
}
@import url("//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css");
.glyphicon {
margin-right: 30px;
margin-top: 4px;
float: right;
}
.glyphicon:hover {
color: red;
}
.hidden {
visibility: hidden;
}
#logo {
margin-bottom: 30px;
}
#logo h1 {
margin: 0;
padding-bottom: 0;
}
#logo p {
margin: 0;
}
#wrapper {
border-style: solid;
width: 340px;
overflow: hidden;
margin: auto auto;
}
#newItem {
float: right;
margin-right: 20px;
margin-bottom: 20px;
}
#textField {
float: left;
margin-left: 20px;
}
#listItems {
margin-bottom: 30px;
text-align: left;
font-size: 22px;
}
.complete {
text-decoration: line-through;
}
<html>

<head>
<link rel="stylesheet" href="style.css" />
<title>The little to do</title>
<meta carset="utf-8" />
<!-- Summon Fonts & Library-->
<link href='https://fonts.googleapis.com/css?family=Indie+Flower' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Cabin' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css">
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css" rel="stylesheet">


</head>

<body>
<div id="draggable">
<p>I'm draggable!</p>
</div>
<div id="wrapper">
<div id="logo">
<h1>Project Bacon</h1>
<p>The Electronic Shopping List</p>
</div>
<!--end logo-->

<div id="listTitle">
<h2>BUY GROCERIES</h2>
<div id="listItems">
<ul>
</ul>
</div>
<!--end listItems-->

<form id="textAddForm">
<div>
<input id="textField" type="text" name="entry" placeholder="Add item...">
</div>
<div id="addBtn">
<input type="submit" name="addBtn" value="Add" type="button">
</div>
</form>
<!--end textAddForm-->

<div id="newItemForm">
<button id="newItem" type="button">New Item</button>
</div>
<!--end newItemForm-->

</div>
<!--end listTitle-->




</div>
<!--end wrapper-->

<!--Summon JS-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>

</html>

关于jQuery-如何仅针对单个列表项进行删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37245524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com