jquery sortable:排序的项目触发重新排序
我有一个代表 div
层的项目列表。当我对这些列表项之一进行排序时,我希望它们各自的 div 层也被排序。
列表:这些项目是可排序的
<ul id="sortable">
<li id="1">Div 1</li>
<li id="2">Div 2</li>
<li id="3">Div 3</li>
</ul>
div 层:这些 div 将重新排序
<div id="div_container">
<div id="div1">Div 1 item</div>
<div id="div2">Div 2 item</div>
<div id="div3">Div 3 item</div>
</div>
示例:当 li#1
移动到第二个位置时,则 div# 1
自动转到第二个位置
初始化
$('#sortable').sortable();
I have a list of items that represent div
layers. When I sort one of these list items, I want their respective div layers to be sorted aswell.
list: these items are sortable
<ul id="sortable">
<li id="1">Div 1</li>
<li id="2">Div 2</li>
<li id="3">Div 3</li>
</ul>
div layers: these divs will be reordered
<div id="div_container">
<div id="div1">Div 1 item</div>
<div id="div2">Div 2 item</div>
<div id="div3">Div 3 item</div>
</div>
example: when li#1
moves to the second place, then div#1
goes to the second position automatically
init
$('#sortable').sortable();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我得到您所要求的内容,此代码可能就是您想要的:
http://jsfiddle.net/NsawH/ 84/
此代码是可移植的,因为它不使用元素 ID,但是您应该参数化可排序选择器以便能够在任意两个列表上使用它们,例如。如果您在 init 之后绑定到可排序。
该代码是 jQuery dom 修改友好的,因为它使用选择器索引而不是节点 dom 索引。您将在 JSFiddle 上看到我将 div_container 设为可排序,并且它同步回列表。
This code could be what you want if I got what you are asking for:
http://jsfiddle.net/NsawH/84/
This code is portable since it does not use element ID's, however you should parametrize the sortable selector to be able to use them on any two lists eg. if you are binding to the sortable after init.
The code is jQuery dom modification friendly since it uses selector indexes and not node dom indexes. You will see on JSFiddle that i made the div_container a sortable , and it syncs back to the list.
绑定可排序的
change
事件(如果您想要实时更新)或stop
(仅读取最终状态),并相应地手动对 div 重新排序。Bind the
change
event of the sortable (if you want real-time updates) orstop
(to just read off the end state), and manually reorder the divs accordingly.