jQuery UI Sortable: How do I prevent a specific list item from being moved?

时光总嘲笑我的痴心妄想 提交于 2019-12-21 02:26:16

问题


I have this list:

<ul>
    <li class="line"><a href="#" class="drag">header (do not sort)</a></li>
    <li class="line"><a href="#" class="drag">line one</a></li>
    <li class="line"><a href="#" class="drag">line two</a></li>
</ul>

How can I prevent the first li from being moved?


回答1:


You can specify which items within the Sortable are actually sortable. This should do the trick—change the HTML as follows:

<ul>
    <li class="line" id="header"><a href="#" class="drag">header (do not sort)</a></li>
    <li class="line"><a href="#" class="drag">line one</a></li>
    <li class="line"><a href="#" class="drag">line two</a></li>
</ul>

Then when you set up the sortable:

$('ul').sortable({ items: 'li[id!=header]' });

Hope this helps!




回答2:


You can explicitly exclude items (aside by not including them):

$( ".sortable" ).sortable({
    cancel: ".disable-sort-item"
});


来源:https://stackoverflow.com/questions/2006401/jquery-ui-sortable-how-do-i-prevent-a-specific-list-item-from-being-moved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!