【发布时间】:2017-11-11 04:20:56
【问题描述】:
我正在尝试让 jQuery 下拉菜单同时在移动设备和桌面设备(触摸和非触摸设备)上工作。
一切都按预期工作,但我无法让“选定”类打开/关闭。另外,如果您对提高性能有任何建议,请告诉我。
$(function(){
$('li.nav__main__item--has-children > a').on('click',function(event){
event.preventDefault();
$(this).toggleClass('selected');
$(this).parent().find('ul').first().toggle(300);
$(this).parent().siblings().find('ul').hide(200);
//Hide menu when clicked outside
$(this).parent().find('ul').on('mouseleave touchend',(function(){
var thisUI = $(this);
$('html').click(function(){
thisUI.hide();
$('html').unbind('click');
});
}));
});
});
这是带有完整示例的 Codepen:https://codepen.io/friendlywp/pen/WXpXWv
感谢您的帮助。
【问题讨论】: