我有一个javascript菜单,适用于Firefox,IE,Safari和iPhone / iPad iOS 4。
仅在iOS 5更新中,子菜单显示非常短暂,然后在单击菜单项时消失。有谁知道他们改变了什么以及如何解决这个问题?
var dbMenu = {
init: function(){
var uls = document.getElementsByTagName('ul');
for(var i = 0; i < uls.length; i++){
if(uls[i].className.search(/\bdbMenu\b/) == -1)
continue;
var menu = uls[i];
dbMenu.styleSubMenus(menu);
addEvent(menu, 'mouseover', dbMenu.hover, false);
addEvent(menu, 'mouseout', dbMenu.hoverOff, false);
if(menu.className.search(/\bonMouse\b/) == -1){
addEvent(menu, 'click', dbMenu.click, false);
}
addEvent(menu, 'click', dbMenu.nav, false);
}
},
hover: function(e){
var target = (window.event)? window.event.srcElement : (e)? e.target : null;
if(target){
target = dbMenu.getTarget(target, 'li');
if(!target) return;
}else{
return;
}
target.className += ' hover';
var t = (target.className.search(/\bsubMenu\b/) != -1)? target : (target.parentSubMenu)? target.parentSubMenu : null;
if(!t) return;
clearTimeout(t.timeout);
if(target.parentMenu.className.search(/\bonMouse\b/) != -1){
t.className += ' click';
}
},
hoverOff: function(e){
var target = (window.event)? window.event.srcElement : (e)? e.target : null;
if(target){
target = dbMenu.getTarget(target, 'li');
if(!target) return;
}else{
return;
}
target.className = target.className.replace(/hover/g, '');
if(target.parentMenu.className.search(/\bonMouse\b/) != -1){
var t = (target.className.search(/\bsubMenu\b/) != -1)? target : (target.parentSubMenu)? target.parentSubMenu: null;
if(!t) return;
t.timeout = setTimeout(function(){ t.className = t.className.replace(/click/g, ''); }, 80);
}
},
click: function(e){
if(window.event){
window.event.cancelBubble = true;
}
if(e && e.stopPropagation){
e.stopPropagation();
}
var target = (window.event)? window.event.srcElement : (e)? e.target : null;
if(target){
target = dbMenu.getTarget(target, 'li');
if(!target) return;
}else{
return;
}
if(target.className.search(/\bclick\b/) == -1){
target.className += ' click';
}else{
target.className = target.className.replace(/click/g, '');
}
},
nav: function(e){
if(window.event){
window.event.cancelBubble = true;
}
if(e && e.stopPropagation){
e.stopPropagation();
}
var target = (window.event)? window.event.srcElement : (e)? e.target : null;
if(target){
target = dbMenu.getTarget(target, 'li');
if(!target) return;
}else{
return;
}
for(var i = 0; i < target.childNodes.length; i++){
var node = target.childNodes[i];
if(node.nodeName.toLowerCase() == 'a'){
window.location = node.href;
break;
}
}
},
getTarget: function(target, elm){
if(target.nodeName.toLowerCase() != elm && target.nodeName.toLowerCase() != 'body'){
return dbMenu.getTarget(target.parentNode, elm);
}else if(target.nodeName.toLowerCase() == 'body'){
return null;
}else{
return target;
}
},
styleSubMenus: function(menu){
lis = menu.getElementsByTagName('li');
for(var i = 0; i < lis.length; i++){
node = lis[i];
node.parentMenu = menu;
if(node.getElementsByTagName('ul').length != 0){
node.className += ' subMenu';
sublis = node.getElementsByTagName('li');
for(var j = 0; j < sublis.length; j++){
sublis[j].parentSubMenu = node;
}
}
}
}
}
function addEvent(elm, evType, fn, useCapture){ //cross-browser event handling for IE5+, NS6+, and Mozilla/Gecko By Scott Andrew
if(elm.addEventListener){
elm.addEventListener(evType, fn, useCapture);
return true;
}else if(elm.attachEvent){
var r = elm.attachEvent('on' + evType, fn);
return r;
}else{
elm['on' + evType] = fn;
}
}
addEvent(window, 'load', dbMenu.init, false);
答案 0 :(得分:0)
第一个也是最重要的事情是触控设备没有点击事件。 Mobile Safari模拟click,mouseover和mouseout事件。 Apple可能已经改变了它在iOS 5中确定点击的方式。
检查以确保悬停处理程序未捕获事件。我首先启用developer console on your iOS device并为每个事件添加日志记录。这应该告诉你什么事件被解雇以及什么时候被解雇。
要修复它,您可能需要检查设备是否支持触摸事件。像"ontouchstart" in window
这样的东西应该可以解决这个问题,并且只绑定该设备所需的事件(悬停或点击)。