0
如何将位置交给同一父母的孩子?
我使用这种模式的代码解决。 有没有更简单的方法?例如这样的事情:$("#id1").exchange($("#id2"));
jQuery - 交换元素位置
<!DOCTYPE html>
<html>
<head>
<style>
button { display:block; margin:3px; color:red; width:200px; }
</style>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<h1>Exchange places</h1>
<ul>
<li id="a">a</li>
<li id="b">b</li>
<li id="c">c</li>
<li id="d">d</li>
</ul>
<button>exchange 1st,2nd</button>
<button>exchange 1st,4th</button>
<button>exchange 2nd,3rd</button>
<script>
$("button:first").click(function() {
var cp = $("li:eq(0)").clone();
$("li:eq(0)").replaceWith($("li:eq(1)").clone());
$("li:eq(1)").replaceWith(cp);
});
$("button:eq(1)").click(function() {
var cp = $("li:eq(0)").clone();
$("li:eq(0)").replaceWith($("li:eq(3)").clone());
$("li:eq(3)").replaceWith(cp);
});
$("button:last").click(function() {
var cp = $("li:eq(1)").clone();
$("li:eq(1)").replaceWith($("li:eq(2)").clone());
$("li:eq(2)").replaceWith(cp);
});
</script>
</body>
</html>
谢谢您的回复。在我的项目中,李元素是带有“向上移动”“向下移动”链接的div元素。对于我的情况,因为我不需要拖动功能,所以我确实不需要额外的“外挂程序”。 – Mattia 2010-08-08 22:31:42