如何在事件的函数中引用元素的子元素?
这种方式不起作用:
$(function() {
$("#sortable1").sortable({
start: function() {
$('#sortable1:first-child').addClass('some')
},
});
});
答案 0 :(得分:2)
为每个可排序项目分配一个类: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
类。
答案 1 :(得分:1)
试试这个:
$('#sortable1').children().eq(0).addClass('some')
答案 2 :(得分:1)
$('#sortable1:first-child')
指向身份document
sortable1
(我认为)的第一个孩子。你需要指定孩子是什么。
例如,如果是span
则需要$('#sortable1 span:first-child')