简体   繁体   English

如何获取在拖动事件 jqueryui/jquery 中单击了哪个 div/ui.helper?

[英]How to get which div/ui.helper clicked in drag event jqueryui/jquery?

On Jquery UI's site:在 Jquery UI 的网站上:

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

If I have:如果我有:

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

And:和:

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

How do I get the id or class of the ID using the "ui" variable from the callback?如何使用回调中的“ui”变量获取 ID 的 ID 或 class? If not possible, how do I get it from the "event" variable?如果不可能,我如何从“事件”变量中获取它?

You want:你要:

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

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

Note : ui.helper is a jQuery object, which is why we must either use .attr("...") to retrieve the id or access the matched element at index 0 and directly get the id.注意ui.helper是 jQuery object,这就是为什么我们必须使用.attr("...")来检索id或访问索引 0 处的匹配元素并直接获取 id。


Or without using the ui argument (probably what I'd recommend):或者不使用ui参数(可能是我推荐的):

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

Here's a working example: http://jsfiddle.net/andrewwhitaker/LkcSx/这是一个工作示例: http://jsfiddle.net/andrewwhitaker/LkcSx/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM