我正在開(kāi)發(fā)一本字典,當(dāng)我開(kāi)始輸入時(shí)它會(huì)自動(dòng)完成,它會(huì)顯示以該字母開(kāi)頭的單詞列表。(如詞匯表網(wǎng)站)searchInput.addEventListener('keyup',function(){ let search = $('#search').val() $.ajax({ type: 'GET', url: '/home/autocomplete/' + search dataType: "json", success: function (response) { let word = response.words suggestionsPanel.innerHTML = ''; $.each(word, function (idx, w) { $('.suggestions').append(`<li name = ${w}>${w}</li>`) // $('.suggestions li:first').addClass('selected')})};現(xiàn)在我想添加向下箭頭鍵來(lái)瀏覽列表。我添加了另一個(gè)按鍵事件監(jiān)聽(tīng)器,它會(huì)繼續(xù)執(zhí)行第一個(gè)事件監(jiān)聽(tīng)器函數(shù)。如何使其兩者都發(fā)揮作用?var li = $('.suggestions > li'); var liSelected; $(window).keydown(function(e) { if(e.which === 40) { if(liSelected) { liSelected.removeClass('selected'); next = liSelected.next(); if(next.length > 0) { liSelected = next.addClass('selected'); } else { liSelected = li.eq(0).addClass('selected'); } } else { liSelected = li.eq(0).addClass('selected'); } } else if(e.which === 38) { if(liSelected) { liSelected.removeClass('selected'); next = liSelected.prev(); if(next.length > 0) { liSelected = next.addClass('selected'); } else { liSelected = li.last().addClass('selected'); } } else { liSelected = li.last().addClass('selected'); } } });
如何從事件偵聽(tīng)器中排除向下箭頭鍵
UYOU
2023-08-05 20:59:24