【问题标题】:Press Esc button to close suggestion list按 Esc 按钮关闭建议列表
【发布时间】:2014-01-15 03:35:18
【问题描述】:

当用户按下Esc按钮时,我希望建议列表将被关闭。

我该怎么做?

这是我的代码:

  1. script_suggestion.php:

    <script type="text/javascript">
    
        //document.getElementById("suggestion")
    
        function getSuggestion(q) {
            var ajax;
            if(window.XMLHttpRequest)//for ie7+, FF, Chrome
                ajax = new XMLHttpRequest();//ajax object
            else
                ajax = new ActiveXObject("Microsoft.XMLHTTP");//for ie6 and previous
            ajax.onreadystatechange = function() {
                if(ajax.status === 200 && ajax.readyState === 4) {
                    //if result are not there then don't display them
                    if(ajax.responseText === "")
                        document.getElementById("suggestion").style.visibility = "hidden";
                    else {
                        document.getElementById("suggestion").style.visibility = "visible";
                        document.getElementById("suggestion").innerHTML = ajax.responseText;
                    }
                }
            };
            ajax.open("GET", "suggestion.php?q=" + q, false);
            ajax.send();
        }
    </script>
    

  2. PHP 代码:

    <?php include 'script_suggestion.php'; include 'script_close_suggestion_box.php'; ?>

提前致谢。

【问题讨论】:

  • 查找 esc 按钮的键码,添加功能以关闭列表。

标签: javascript jquery search-engine


【解决方案1】:

使用这个:

var forceClose = false;

function getSuggestion(q) {

    if (!forceClose) {

        var ajax;
        if (window.XMLHttpRequest)//for ie7+, FF, Chrome
            ajax = new XMLHttpRequest();//ajax object
        else
            ajax = new ActiveXObject("Microsoft.XMLHTTP");//for ie6 and previous
        ajax.onreadystatechange = function () {
            if (ajax.status === 200 && ajax.readyState === 4) {
                //if result are not there then don't display them
                if (ajax.responseText === "")
                    document.getElementById("suggestion").style.visibility = "hidden";
                else {
                    document.getElementById("suggestion").style.visibility = "visible";
                    document.getElementById("suggestion").innerHTML = ajax.responseText;
                }
            }
        };
        ajax.open("GET", "suggestion.php?q=" + q, false);
        ajax.send();

    }
}

window.document.onkeydown = function (e) {
    if (!e) e = event;
    if (e.keyCode == 27) {
        document.getElementById("suggestion").style.visibility = "hidden";
        forceClose = true;
    }
};

【讨论】:

  • 当我插入此代码时,我尝试按Esc。当我按下它时,建议列表只是闪烁,它没有关闭。我该如何解决这个问题?
【解决方案2】:

在 getSuggestion() 函数后添加这段代码:

window.document.onkeyup = function (e)
{
if (!e) e = event;
if (e.keyCode == 27)
document.getElementById("suggestion").style.visibility = "hidden";
}

【讨论】:

    猜你喜欢
    • 2015-01-08
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 2018-06-25
    • 2017-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多