我想在jQuery中做一個ajax搜索欄。我在一個地方觸動過。我在一個大div元素內創建了動態div元素,意味着創建了與json響應一起的div。我想要在用戶輸入東西時遍歷div元素那麼它應該通過上下箭頭鍵來遍歷。如何遍歷div元素在jquery中的div元素?
<div class="comment">
<div class="single" >
//this div is dynamically created with the information
</div>
</div>
我想突出div元素內的項目與鍵和鍵的幫助下。 什麼是可能的方式。我嘗試了2-3種方法,但沒有用。
編輯 這是我曾嘗試
$(document).ready(function(){
$('#inputText').on('keydown',function(e){
var $current=$('.comment>.single').first();
$current.css('background-color','yellow');
if(e.keyCode==40){
var $nextDiv;
if($current.next().size==0){
$nextDiv=$('.comment>.single').first();
}
else
{
$nextDiv=$current.next();
}
$current.css('background-color','');
$nextDiv.css('background-color','yellow');
$current=$nextDiv;
}
});
});
謝謝您的幫助
你試過的2-3種方法是什麼? –
我曾嘗試使用過濾器,第一個功能。但我可以輕鬆地突出顯示2div和其餘div元素,但當用戶從輸入框中按下1次時,我在第一個div處被敲擊,然後它應該首先突出顯示,然後它應該移動休息。 – Andrew
在這裏發佈你的代碼...沒有看到你想要做的事情,人們將無法提供幫助 –