我有一个简单的待办事项列表作为我的第一个项目。当点击删除列表中的最后一个项目时,我想让整个灰色DIV消失,而是保留一个空白框。我在这里阅读了许多答案,但似乎没有任何工作适合于我的情况,或者我做错了,更可能。删除最后一项后无法隐藏div
我有https://codepen.io/HelleFl/pen/OjNQop
$(function() {
// Hide the container upon ready
$(".list-container").hide();
//* Prevent empty Add from continuing function by Evaluation; It will not accept only spaces; Clear input list once add is clicked; add item & Font Awesome icon to list *//
$("#button").click(function() {
if ($("input[name=checkListItem]").val().trim() !== "") {
var toAdd = $("input[name=checkListItem]").val();
$(".list-container").fadeIn(500);
$(".list").append(
'<div class="item"><i class="fa fa-times" aria-hidden="true"/>' +
toAdd +
"</div>"
);
}
// Focus back on text input once add is clicked
$("input[name=checkListItem]").val("").focus();
// click the X icon to remove that item
$(document).on("click", ".fa", function() {
$(this).parent().remove();
});
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<div class='form'>
<input type="text" name="checkListItem" class='hover-box' placeholder='Enter your shenanigans' />
<button class='btn btn-primary' id="button">Add!</button>
<hr>
</div>
<div class='list-container'>
<h3>My to-do list</h3>
<div class='container'>
<ul class='list'>
</ul>
</div>
</div>
</div>
可能重复[隐藏div如果它们为空](https://stackoverflow.com/questions/5327751/hide-divs-if-they-are-empty) – Ivar
只要删除'padding-top:1em ;'从'.container'并且其次添加'ul {margin:0;填充:0}' –
@milanChheda我可以做到这一点,但当它包含项目时,列表看起来非常糟糕 – Helle