单击以在两个列表之间交换元素

心智主义者

我正在使用SortableJS构建菜单,其中可以将标签项之类的哈希标签从搜索结果区域拖到将对其计数的另一个区域。我已经在两个列表之间使用了拖放功能。现在,我要做的是添加单击功能,以便单击一个列表中的标记项时,它会移到相反的列表。

这应该可以帮助不需要拖放或宁愿单击的用户。目标是容纳广泛的用户设备(移动/台式机)和计算机知识(精通技术/不精通技术)。

这是使用SortableJS的共享列表的示例。

SortableJS共享列表。 这些项目可以在列表之间拖放,但是我希望它们是可单击的。

Both lists share the class list-group, which enables them to share items with each other.

I've been looking into the possibility of creating a script that gets the div element that is clicked on (from either list), removes it, and instantly re-adds to the opposite list, with all the div attributes and contents in tact. But maybe modifying the existing SortableJS code is a better approach?

-- As a quick test, I bypassed the UI and changed the order of the items purely in HTML using Developer Tools, and it didn't break the functionality. So doing the same with a script should work.

Currently I am researching how to have any item of a given class, when clicked on, removed from a parent element and then made a child of another parent element (the other list).

-我找到了一个简单而优雅的jQuery脚本,对于这种事情似乎很有希望。我开始修改它以支持两个列表,但是不幸的是,多次交换后,它无法正常工作。希望可以进行一些修改使其工作。

<!DOCTYPE html>
<html>
<head>
<style>
	div {
		width: 180px;
		padding: 2px 12px;
		margin-bottom: 10px;
	}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
</head>
<body>

<div id="div1" style="background-color: #a55">
<p class="swappable">Paragraph one.</p>
<p class="swappable">Paragraph two.</p>
</div>

<div id="div2" style="background-color: #bab1e5">
<p class="swappable">Paragraph three.</p>
<p class="swappable">Paragraph four.</p>
</div>

<script>
$("#div1 p").click(function() {
	
	$("#div2").append($(this));
	
});

$("#div2 p").click(function() {
	
	$("#div1").append($(this));
	
});
</script>

</body>
</html>

浏览文档中的jQuery的.append(),我不知道自己在做什么错。

Parth Jasani |

我认为您应该使用jQuery的点击方法

    <!DOCTYPE html>
<html>
<head>
<style>
    div {
        width: 180px;
        padding: 2px 12px;
        margin-bottom: 10px;
    }
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
</head>
<body>

<div id="div1" style="background-color: #a55">
<p class="swappable">Paragraph one.</p>
<p class="swappable">Paragraph two.</p>
</div>

<div id="div2" style="background-color: #bab1e5">
<p class="swappable">Paragraph three.</p>
<p class="swappable">Paragraph four.</p>
</div>

<script>
$("#div1").on("click","p",function() {
    $("#div2").append($(this));

});

$("#div2").on("click","p",function() {
    $("#div1").append($(this));
});
</script>

</body>
</html>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章