jquery - jQuery - 使用 click 函数内部的每个移动 div
问题描述
我想将.move
div放置在单击.destination
时的内部,否则单击if的.gridview
内部。.item
.listview
当前,当单击任一视图时,.move
div 会被迭代多次。如何更改代码以便.move
每个 div 附加一次.item
?
$(".gridview").on("click", function() {
$(".item").each(function() {
var $this = $(this);
$this.find(".move").appendTo(".destination");
});
var $this = $(this).closest(".container");
$this.find(".listview").removeClass("active");
$this.find(".gridview").addClass("active");
$this.find(".block").css("display", "flex");
});
$(".listview").on("click", function() {
$(".item").each(function() {
var $this = $(this);
var $list = $this.closest(".list");
$this.find(".move").appendTo(".item");
});
var $this = $(this).closest(".container");
$this.find(".gridview").removeClass("active");
$this.find(".listview").addClass("active");
});
.item {
margin: 1.3rem 0;
border: 2px solid;
}
.destination {
height: 35px;
border: 3px solid
}
.move {
height: 25px;
border: 2px solid;
background: green
}
.block {
display: none;
height: 45px;
width: 45px;
border: 2px solid green;
}
.active {
background: blue;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container">
<div class="gridview">Grid</div>
<div class="listview active">List</div>
<div class="list">
<div class="item">
<div class="destination"></div>
<div class="block"></div>
<div class="move"></div>
</div>
<div class="item">
<div class="destination"></div>
<div class="block"></div>
<div class="move"></div>
</div>
</div>
</div>
解决方案
你可以试试下面的代码吗?
$(".gridview").on("click", function() {
$.each($(".list .item"),function(i,v){
$(this).find(".destination").append($(v).find(".move"))
})
var $this = $(this).closest(".container");
$this.find(".listview").removeClass("active");
$this.find(".gridview").addClass("active");
$this.find(".block").css("display", "flex");
});
$(".listview").on("click", function() {
$.each($(".list .item"),function(i,v){
$(this).append($(v).find(".destination .move"))
})
var $this = $(this).closest(".container");
$this.find(".gridview").removeClass("active");
$this.find(".listview").addClass("active");
});
推荐阅读
- php - 注意:试图获取非对象的属性'num_rows'(数据库为空!请先注册一些员工)
- python - 包含部分字符串的数组中的 iem 数
- javascript - 当我在反应中重新排序表格时,“孩子已经有父母,必须先将其移除”
- java - 减去时间时我总是得到 0
- javascript - 循环获取请求在最后一项之前返回错误
- sql - 日期类型的查询 substr 的索引是什么?
- kubernetes - 当您的主容器完成其工作时,如何终止同一个 pod 中的另一个容器
- java - 构建API的时候同步一个方法对吗
- c# - INotifyDataErrorInfo - 模型或 ViewModel
- mpdf - MPDF 错误消息:必须在表格中位于前面