javascript - 如何移动一个
- 在另一个
- 在使用 JQuery 的单独 div 中
问题描述
所以我有两个容器,其中包含单独ul
的 . 我的目标是将其中一个孩子<li>
从一个ul
转移到另一个。
我目前使用 jquery 在 ul 中的 li 上有一个删除功能,如下所示:
//check off specific todos
$("ul").on("click", "li", function(){
$(this).toggleClass("completed");
});
// x to delete todo /mark as complete
$("ul").on("click", "span", function(event){
$(this).parent().fadeOut(500, function(){
$(this).remove();
});
event.stopPropagation();
});
//text input
$("input[type='text']").keypress(function(event){
if(event.which === 13){
var todotext = $(this).val();
$(this).val("");
// create a new LI
$("ul").append("<li><span><i class='fas fa-trash'></i></span> " + todotext + "</li>");
}
});
//toggles input box
$(".fa-plus").click(function(){
$("input[type='text']").fadeToggle();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.1/css/all.min.css">
<body>
<div id="container">
<h1>Task List <i class="fas fa-plus"></i></h1>
<input placeholder="Add new Task" type="text">
<ul>
<li><span><i class="fas fa-check"></i></span> hello</li>
</ul>
</div>
<div id="container-2">
<h1>Completed Tasks <i class="fas fa-check-circle"></i></h1>
<ul>
</ul>
</div>
</body>
我对此真的很陌生,所以只是想个性化一个项目。
解决方案
您应该将元素附加到目标容器,而不是删除,然后使用fadeIn
它再次显示它。
//check off specific todos
$("ul").on("click", "li", function(){
$(this).toggleClass("completed");
});
// x to delete todo /mark as complete
$("ul").on("click", "span", function(event){
$(this).parent().fadeOut(500, function(){
$(this).appendTo($("#container-2 ul")).fadeIn(500);
});
event.stopPropagation();
});
//text input
$("input[type='text']").keypress(function(event){
if(event.which === 13){
var todotext = $(this).val();
$(this).val("");
// create a new LI
$("ul").append("<li><span><i class='fas fa-trash'></i></span> " + todotext + "</li>");
}
});
//toggles input box
$(".fa-plus").click(function(){
$("input[type='text']").fadeToggle();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.1/css/all.min.css">
<body>
<div id="container">
<h1>Task List <i class="fas fa-plus"></i></h1>
<input placeholder="Add new Task" type="text">
<ul>
<li><span><i class="fas fa-check"></i></span> hello</li>
</ul>
</div>
<div id="container-2">
<h1>Completed Tasks <i class="fas fa-check-circle"></i></h1>
<ul>
</ul>
</div>
</body>
推荐阅读
- django - 使用 ForeignKey、Django Subquery 和 OuterRef 将一个模型中的字段合并到另一个模型中
- ffmpeg - ffmpeg:通过添加黑框将纵向和横向视频文件合并到一个文件中
- jquery - 为什么我的 jquery 代码在移动设备上不起作用?
- swiftui - SwiftUI:LazyVGrid 滚动时不刷新
- javascript - 如何在单选按钮上使用 JavaScript 逻辑?
- php - 统计通过分数的用户
- python - Django 更改 HTML 模板不反映
- ethereum - 支持来自父合约的多个代理合约
- swift - 无法将“ServiceModel.Type”类型的值转换为预期的参数类型“ServiceModel”
- javascript - 在现有状态转换错误期间无法更新