如何使单个组合框可排序?

时间:2019-08-06 07:44:27

标签: jquery jquery-ui

我正在尝试使单个组合框可排序。有关可排序项的Jquery UI文档提到,应按以下方式设置可排序项:

$( ".selector" ).sortable();

当我放入选择器后,它使我所有的Select2元素都可以排序:

$("ul.select2-selection__rendered").sortable()

我想这样做,因此特定div中只有一个元素是可排序的。我尝试从浏览器复制CSS选择器路径,但出现错误:

var outpatient = document.querySelectorAll('#department-outpatient > div:nth-child(2) > div:nth-child(2) > div:nth-child(2) > span:nth-child(2) > span:nth-child(1) > span:nth-child(1) > ul:nth-child(1)')[0];
outpatient.sortable({
  // ...
});
  

TypeError:outpatient.sortable不是函数

1 个答案:

答案 0 :(得分:0)

在使用outpatient进行检索时,querySelectorAll()变量将保存一个Element对象。 sortable()依赖于jQuery,因此需要在jQuery对象上调用。因此,要解决此问题,您可以从outpatient创建一个jQuery对象,然后在其上调用sortable()

var outpatient = document.querySelectorAll('#department-outpatient > div:nth-child(2) > div:nth-child(2) > div:nth-child(2) > span:nth-child(2) > span:nth-child(1) > span:nth-child(1) > ul:nth-child(1)')[0];
$(outpatient).sortable({
  // ...
});

更进一步,使用jQuery本身选择元素。另请注意,我确定您可以简化该选择器。似乎太具体了。

var $outpatient = $('#department-outpatient > div:nth-child(2) > div:nth-child(2) > div:nth-child(2) > span:nth-child(2) > span:nth-child(1) > span:nth-child(1) > ul:nth-child(1)');
$outpatient.sortable({
  // ...
});
相关问题