2011-04-30 13 views
2

在jQuery用戶界面的網站:如何獲取哪個div/ui.helper在拖動事件jqueryui/jquery中單擊?

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? 
    } 
}); 

如何獲得使用該ID的ID或類「 ui「變量從回調?如果不可能,我如何從「事件」變量中獲取它?

回答

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/

相關問題