如您所知,在 Android 上,对于除锚标记之外的所有其他元素,只需调用 click() 即可。在 Android 上,对于锚标签,您需要在该元素上触发开始和结束触摸事件。来源(显示的打字稿 - Javascript 的超集):
// 在 elementX 上触发点击(jquery 将解释为点击)的示例调用:
Gremlins.tap([{ x: 1, y: 1 }], document.getElementById('yourAnchorId'));
/**
* trigger a touchevent
* @param touches Array of touch positions within the element (for a sequence of touches). Ex: [{ x: 1, y: 1}]
* @param element
*/
private static tap(touches, element) {
Gremlins.triggerTouch(touches, element, 'start');
setTimeout(function () {
Gremlins.triggerTouch(touches, element, 'end');
}, 50);
}
/**
* trigger a touchevent
* @param touches Array of touch positions within the element (for a sequence of touches). Ex: [{ x: 1, y: 1}]
* @param element
* @param type - "start" or "end" of the process of 1 tap event
*/
private static triggerTouch(touches, element, type) {
var touchlist = [],
event:any = document.createEvent('Event');
event.initEvent('touch' + type, true, true);
touches.forEach(function (touch, i) {
var x = Math.round(touch.x),
y = Math.round(touch.y);
touchlist.push({
pageX: x,
pageY: y,
clientX: x,
clientY: y,
screenX: x,
screenY: y,
target: element,
identifier: i
});
});
event.touches = (type == 'end') ? [] : touchlist;
event.targetTouches = (type == 'end') ? [] : touchlist;
event.changedTouches = touchlist;
element.dispatchEvent(event);
}