好的,那就是发生了这样的事情。
$('.js-custom-dropdown').find('.custom-dropdown-unfolded').toggle();
$('.custom-dropdown-btn, .custom-dropdown-btn-unfolded').keydown(function(event){
if (event.keyCode === 13) {
openDropdown($(this));
}
}).click( function(){
openDropdown($(this));
});
function openDropdown (element){
element.parents('.js-custom-dropdown').find('.custom-dropdown-unfolded').toggle();
console.log($(this))
}
当我点击下拉按钮时,
openDropdown
函数只执行一次,但当我使用Tab键切换到该按钮并按下Enter键时,该函数会被调用两次。猜测这可能与链接有关,但我承认我是新手,不完全了解jQuery设计模式。当然,我可以在keydown
处理程序中调用该函数两次,这样就可以解决问题,但是...你懂的 :)请问您能否解释一下代码有什么问题以及是什么原因导致这种行为?