2

在 Jquery UI 的网站上:

http://jqueryui.com/demos/draggable/

如果我有:

<div id="someId" class="someClass">he</div>
<div id="otherId" class="otherClass">he2</div>

和:

$('#someid','#otherid').draggable({
    drag: function(event, ui) {
      alert(ui.helper.THEIDOFTHECLICKEDITEM); // What goes here?
    }
});

如何使用回调中的“ui”变量获取 ID 的 ID 或类?如果不可能,我如何从“事件”变量中获取它?

4

1 回答 1

7

你要:

$("#someId, #otherId").draggable({
    drag: function(event, ui) {
        console.log(ui.helper[0].id);
    }
});

(或使用ui.helper.attr("id")

注意:ui.helper是一个 jQuery 对象,这就是为什么我们必须使用.attr("...")来检索id或访问索引 0 处的匹配元素并直接获取 id。


或者不使用ui参数(可能是我推荐的):

$("#someId, #otherId").draggable({
    drag: function(event, ui) {
        console.log(this.id); // "this" is the DOM element being dragged.
    }
});

这是一个工作示例:http: //jsfiddle.net/andrewwhitaker/LkcSx/

于 2011-04-30T03:01:25.810 回答