简体   繁体   English

Ajax下拉的键盘导航(向上和向下箭头)

[英]Keyboard navigation (up & down arrow) for Ajax drop down

I followed W3Schools tutorial on making the AJAX live search, and it has been working fine. 我遵循W3Schools教程进行AJAX实时搜索,并且运行良好。 I return my AJAX results as anchor elements. 我返回我的AJAX结果作为锚元素。

I'd like to add keyboard navigation (ie up arrow and down arrow) for the Ajax drop-down, and my best result is to get the focus on the first result that stays only for a second then the focus disappears. 我想为Ajax下拉菜单添加键盘导航(即向上箭头和向下箭头),而我最好的结果是将焦点放在第一个结果上,该结果仅停留一秒钟,然后焦点消失。 I wonder why this focus disappears, and any way to get around it. 我不知道为什么这个焦点消失了,以及如何解决它。

My JavaScript code: 我的JavaScript代码:

<script type="text/javascript">
    $(document).ready(function(){
        $('#searchInput').keyup(function(e){
            var keyCode = e.keyCode || e.which;
            if (keyCode == 40){
                 $('.hint').first().focus();
                 $('.hint').first().css('color','#E8AE00'); //I can get the focus to here, but the focus will disappear right away.
            }
        })
    })
</script>

This is my PHP code: 这是我的PHP代码:

<?php
    $q = $_GET["q"];
    $xmlDoc = new DOMDocument();
    $xmlDoc -> load("database.xml");
    $rest = $xmlDoc -> getElementsByTagName('restaurant');

    if (strlen($q)>0){
        $hint[] = "";
        $index = 0;
        for ($i = 0; $i < ($rest->length); $i++){
            $name = $rest -> item($i) -> getElementsByTagName('name');
            $link = $rest -> item($i) -> getElementsByTagName('link');
            if ($name -> item(0) -> nodeType == 1){
                if (strtolower($q) == strtolower(substr($name -> item(0) -> childNodes -> item(0) -> nodeValue,0,strlen($q)))){ //if matching
                    $hint[$index] = "<a class='hint' id='hint".$index."' href='".$link -> item(0) -> childNodes -> item(0) -> nodeValue."' onfocus=\"this.style.color='#E8AE00'\">".substr($name -> item(0) -> childNodes -> item(0) -> nodeValue,0,strlen($q))."<b>".substr($name -> item(0) -> childNodes -> item(0) -> nodeValue,strlen($q))."</b></a><br />";
                    $index++;
                }
            }
        }
    }

    if ($hint[0] == ""){
        echo "no suggestion";
    }
    else {
        for ($j = 0; $j < (count($hint)); $j++){
            echo $hint[$j];
        }
    }
?>

Thanks. 谢谢。

Probably the dropdown is disappearing because you're calling $('.hint').first().focus(); 下拉列表可能消失了,因为您正在调用$('.hint').first().focus(); , which steals the focus from (aka blurs) #searchInput . ,它从#searchInput (又名模糊)窃取了焦点。 Blurring the input, I assume, hides the dropdown due to some JS code that you didn't include here, which (correctly) is hiding the dropdown. 我认为,由于某些您未在此处包含的JS代码(模糊地)隐藏了下拉列表,因此我认为模糊了输入下拉列表。

I'm not sure why you even need to call focus() on the hint. 我不确定为什么您甚至需要在提示上调用focus()

You're building the links with an inline javascript event on focus, which seems really uneccessary? 您正在建立具有焦点的内联javascript事件的链接,这似乎是不必要的吗?

<a class='hint' id='hint" ...... onfocus=\"this.style.color='#E8AE00'\">"

also, note that you are generating the same ID multiple times? 另外,请注意您多次生成相同的ID? They should be unique! 他们应该是独一无二的!

If you use some jQuery and create a delegated mouse event, and then trigger that event instead of the focus event, it should most likely work ? 如果您使用一些jQuery并创建一个委托鼠标事件,然后触发该事件而不是focus事件,则它最有可能工作吗?

$(function(){
    $(document).on({
        mouseenter: function() {
            $(this).css('color', '#E8AE00')
        }
    }, '.hint');

    $('#searchInput').on('keyup', function(e){
            if (e.which == 40){
                 $('.hint').first().trigger('mouseenter');
            }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM