如何在事件上引用函数中的元素子元素?
这种方式不起作用:
$(function() {
$("#sortable1").sortable({
start: function() {
$('#sortable1:first-child').addClass('some')
},
});
});
每个可排序项目都分配有一个类:ui-sortable-handle
。然后,您可以在start
回调中选择此选项。
$(function() {
$("#sortable1").sortable({
start: function() {
$(this).find(".ui-sortable-handle").eq(0).addClass('some');
/* OR:
$(this).find(".ui-sortable-handle").filter(":first").addClass('some');
*/
}
});
});
另请参阅: http://api.jqueryui.com/sortable/
如果移动此项目,则您添加的类将被随身携带。如果稍后的另一个项目是"第一个",它也将被分配some
类。
试试这个:
$('#sortable1').children().eq(0).addClass('some')
$('#sortable1:first-child')
指向document
的第一个孩子(我认为)具有 idsortable1
。您需要指定子项是什么。
例如,如果它是一个span
您需要$('#sortable1 span:first-child')