使用来自 Jquery-ui sortable 的代码示例在基于 Android 或 IOS 的触摸设备上无法在 iOS 设备上触摸启用 jQuery UI sortables,注册 knockout.js 点击处理程序以及 jQuery UI sortable 时出现问题在相同的元素上。knockout.js 处理程序不会在启用触摸的设备上触发,但会在台式机/笔记本电脑上触发。
添加一个名为moved的标志可以跟踪何时需要触发点击处理程序,标记// TRIGGER HERE
如下:
/*
* Content-Type:text/javascript
*
* A bridge between iPad and iPhone touch events and jquery draggable,
* sortable etc. mouse interactions.
* @author Oleg Slobodskoi
*
* modified by John Hardy to use with any touch device
* fixed breakage caused by jquery.ui so that mouseHandled internal flag is reset
* before each touchStart event
*
*/
(function( $ ) {
$.support.touch = typeof Touch === 'object';
if (!$.support.touch) {
return;
}
var proto = $.ui.mouse.prototype,
_mouseInit = proto._mouseInit
moved = true;
$.extend( proto, {
_mouseInit: function() {
this.element
.bind( "touchstart." + this.widgetName, $.proxy( this, "_touchStart" ) );
_mouseInit.apply( this, arguments );
},
_touchStart: function( event ) {
if ( event.originalEvent.targetTouches.length != 1 ) {
return false;
}
this.element
.bind( "touchmove." + this.widgetName, $.proxy( this, "_touchMove" ) )
.bind( "touchend." + this.widgetName, $.proxy( this, "_touchEnd" ) );
this._modifyEvent( event );
$( document ).trigger($.Event("mouseup")); //reset mouseHandled flag in ui.mouse
this._mouseDown( event );
moved = false;
return false;
},
_touchMove: function( event ) {
this._modifyEvent( event );
this._mouseMove( event );
moved = true;
},
_touchEnd: function( event ) {
this.element
.unbind( "touchmove." + this.widgetName )
.unbind( "touchend." + this.widgetName );
this._mouseUp( event );
if (! moved) {
// TRIGGER HERE
}
},
_modifyEvent: function( event ) {
event.which = 1;
var target = event.originalEvent.targetTouches[0];
event.pageX = target.clientX;
event.pageY = target.clientY;
}
});
})( jQuery );
问题是,如何从 jQuery UI 触发对 knockout.js的点击事件?
我试过this.element.click()
, this.element.get().click()
,this.element.trigger("click")
等无济于事。
更新:
将代码破解为:
- 跟踪实际目标,因为 html.element 似乎不是正确的
- 在正确的目标上触发点击事件
现在它可以与 knockout.js 的点击事件一起正常工作。
/*
* Content-Type:text/javascript
*
* A bridge between iPad and iPhone touch events and jquery draggable,
* sortable etc. mouse interactions.
* @author Oleg Slobodskoi
*
* modified by John Hardy to use with any touch device
* fixed breakage caused by jquery.ui so that mouseHandled internal flag is reset
* before each touchStart event
*
*/
(function( $ ) {
$.support.touch = typeof Touch === 'object';
if (!$.support.touch) {
return;
}
var proto = $.ui.mouse.prototype,
_mouseInit = proto._mouseInit
moved = true,
currentTarget = null;
$.extend( proto, {
_mouseInit: function() {
this.element
.bind( "touchstart." + this.widgetName, $.proxy( this, "_touchStart" ) );
_mouseInit.apply( this, arguments );
},
_touchStart: function( event ) {
if ( event.originalEvent.targetTouches.length != 1 ) {
return false
}
this.element
.bind( "touchmove." + this.widgetName, $.proxy( this, "_touchMove" ) )
.bind( "touchend." + this.widgetName, $.proxy( this, "_touchEnd" ) );
this._modifyEvent( event );
$( document ).trigger($.Event("mouseup")); //reset mouseHandled flag in ui.mouse
this._mouseDown( event );
moved = false;
return false;
},
_touchMove: function( event ) {
this._modifyEvent( event );
this._mouseMove( event );
moved = true;
},
_touchEnd: function( event ) {
this.element
.unbind( "touchmove." + this.widgetName )
.unbind( "touchend." + this.widgetName );
this._mouseUp( event );
if (! moved) {
$(currentTarget).click();
}
},
_modifyEvent: function( event ) {
event.which = 1;
var target = event.originalEvent.targetTouches[0];
currentTarget = target.target;
event.pageX = target.clientX;
event.pageY = target.clientY;
}
});
})( jQuery );