JavaScript触发器单击iOS浏览器中的链接不工作
我有这样一个链接:JavaScript触发器单击iOS浏览器中的链接不工作,javascript,android,ios,safari,Javascript,Android,Ios,Safari,我有这样一个链接: <a href="index.html" class="link_to_home" target="_blank" style="display:none;">home</a> 但是它不适用于iOS和Android浏览器。试试这个,普通的点击方法在iOS浏览器上不起作用。改用DOM方法(其跨浏览器) HTML <a href="index.html" id="yourAnchorId" class="link_to_home" target="
<a href="index.html" class="link_to_home" target="_blank" style="display:none;">home</a>
但是它不适用于iOS和Android浏览器。试试这个,普通的点击方法在iOS浏览器上不起作用。改用DOM方法(其跨浏览器) HTML
<a href="index.html" id="yourAnchorId" class="link_to_home" target="_blank" style="display:none;">home</a>
正如您所知,在Android上,对于除锚定标记以外的所有其他元素,只需调用click()即可。在Android上,对于锚定标签,您需要在该元素上触发开始和结束触摸事件。源代码(显示Typescript-Javascript的超集): //在elementX上触发点击(jquery将解释为点击)的示例调用: Gremlins.tap([{x:1,y:1}],document.getElementById('yourAnchorId')
$('.link_to_home')。触发器('click');你如何使用它?试着调试,也许你的js在触发clickmy之前会停止。我的问题是,它在pc上的浏览器中工作,但在android和ios浏览器上的浏览器中不工作。试着使用它,而不是jquery-可能是@user3230788的副本,试着用HTMLEvents重新绘制MouseeEvent,例如document.createEvent(“HTMLEvents”);
<a href="index.html" id="yourAnchorId" class="link_to_home" target="_blank" style="display:none;">home</a>
var ele = document.getElementById('yourAnchorId');
var click_ev = document.createEvent("MouseEvent");
click_ev.initEvent("click", false /* bubble */, true /* cancelable */);
ele.dispatchEvent(click_ev);
/**
* 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);
}