在包含10个li元素的列表中,我通过Sortable.js对列表进行排序。但我需要被拖拽的李所在的物品的id。我怎么才能得到它呢?
发布于 2021-02-21 22:44:03
我使用SortableJs对同一类别列表中的项目进行排序,但也可以将项目从一个类别列表移动到另一个类别列表,反之亦然。
我不确定这是否是您要查找的内容,但这是我获取目的地容器(列表) ID的方法。
event.oldIndex是项目的原始位置item.event.from是项目的原始容器元素(例如,tag)event.newIndex是项目的最终位置item.event.to是项目的最终容器元素(例如,ul标签)惠特说,你可以这样做:
/* If position did not change neither the containing list */
if (event.oldIndex === event.newIndex && event.from === event.to) { return; }
/* If position changed within the same category */
if (event.oldIndex !== event.newIndex) {
const newPosition = event.newIndex
}
/* If product was moved to another category list */
if (event.from !== event.to) {
/* Accessing `<ul data-id="10">` */
const categoryId = event.to.dataset.id;
/* Accessing `<ul id="list_on_the_right_11">` */
const elementId = event.to.id;
}希望它能在某种程度上有所帮助。
https://stackoverflow.com/questions/64053950
复制相似问题