English 中文(简体)
选择带有上箭头/下箭头的 div
原标题:Select div with up/down arrows

我有文字输入, 当我聚焦于它时 - 首先. 选择 # number_ list 成为. sected 。 我想创建一个特性 - 当按住按在按键管上上下键比下键或按住数字列表中前端元素 或按住数字列表中显示选定类别和当前元素的 prev 元素时 。

<input type="text" name="number" id="number" maxlength="30" />

<div id="number_list">
  <div class= option selected >aaa</div>
  <div class= option >bbb</div>
  <div class= option >ccc</div>
</div>

http://jsfiddle.net/YNyEr/

我试图创建. option 元素的数组, 并按索引获取当前元素, 但我找不到任何方法做到这一点 。 所以如何做到这一点? 另外我使用 jQuery 1. 5.1 。

最佳回答

如何快速和肮脏地执行:http://jsfiddle.net/zerkms/YNyEr/2/

var $input = $( #some_number ),
    current_index = $( .selected ).index(),
    $number_list = $( #number_list ),
    $options = $number_list.find( .option ),
    items_total = $options.length;

$input.val(current_index);

$input.bind( keyup , function(e) {
    if (e.keyCode == 40) {
        if (current_index + 1 < items_total) {
            current_index++;
            change_selection();
        }
    } else if (e.keyCode == 38) {
        if (current_index > 0) {
            current_index--;
            change_selection();
        }
    }
});

function change_selection()
{
    $options.removeClass( selected );
    $options.eq(current_index).addClass( selected );
    $input.val(current_index);
}​
问题回答

I think you should have a look at the javascript events onkeyup and onkeydown. If you play with the selection by id it should be quite easy,

例如

<div onkeydown= document.getElementById("nextid").className += "selected";this.className -= "selected";  class= option >bbb</div>




相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签