【发布时间】:2014-05-23 15:10:15
【问题描述】:
在开发拖放应用程序时,我对将事件基于哪些对象感到有些困惑。例如,当我初始化可拖动对象时,我设置了一个函数以在拖动回调上运行并将其传递给 ui,以便我可以跟踪它的位置:
jQuery( '.drag' ).draggable({
helper : 'clone',
drag : function ( event, ui ) { test(ui.helper, ui, this);}
});
function test(helper, drag_ui, drag_this){
//check the type of each, and as expected each are objects
console.log('HLEPER TYPE: '+jQuery.type(cln_h));//object
console.log('UI TYPE: '+jQuery.type(cl));//object
console.log('THIS TYPE: '+jQuery.type(this_elem));//object
//lets try and get the top position of each
console.log('helper.position.top: '+helper.position.top);//undefined
console.log('drag_ui.position.top: '+drag_ui.position.top);//working
console.log('jQuery(drag_this).position.top: '+jQuery(drag_this).position.top);//undefined
//So, the ui parameter gives us a result, ui.helper nothing, and this nothing.
//However, if I get the position() of the helper and this first, then I get results.
//get pos first
helper_pos = helper.position();
console.log('helper_pos.top: '+helper_pos.top);//working
drag_this_pos = jQuery(drag_this).position();
console.log('drag_this_pos.top: '+drag_this_pos.top);//working, but different result
//So now i have some results, but the weird thing is that drag_this is different to ui, and ui.helper.
}
我想要的是在我拖动助手克隆时知道顶部位置。我见过的不同示例使用这三个中的任何一个:ui、ui.helper 和 this。我想知道的是:
- 它们之间有什么区别?它们都是对象,但属性不同?
- 在这种情况下,最佳做法是什么?
期待从中学到一些东西。
谢谢。
【问题讨论】:
标签: jquery jquery-ui drag-and-drop this jquery-ui-draggable