最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get which divui.helper clicked in drag event jqueryuijquery? - Stack Overflow

programmeradmin0浏览0评论

On Jquery UI's site:

/

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? If not possible, how do I get it from the "event" variable?

On Jquery UI's site:

http://jqueryui./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? If not possible, how do I get it from the "event" variable?

Share Improve this question edited Apr 30, 2011 at 3:05 Andrew Whitaker 126k32 gold badges295 silver badges308 bronze badges asked Apr 30, 2011 at 2:52 TesselateTesselate 1312 gold badges4 silver badges8 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You want:

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

(or use 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.


Or without using the ui argument (probably what I'd remend):

$("#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/andrewwhitaker/LkcSx/

发布评论

评论列表(0)

  1. 暂无评论