使用密钥代码在可滚动div中上下导航

时间:2012-08-17 06:37:53

标签: javascript jquery

我有一个可搜索的文本框,用搜索结果填充div。 div是可滚动的。我想要实现的是通过页面向上和向下导航结果项目(键码38和40)。但是一旦我尝试这个,整个div就会滚动,结果项本身不会采用新选择的css类。

以下是我的一些代码

this.TagNavigation = function (event) {
    var div = $("#TagSearchResults");
    var anchors = $("#TagSearchResults a");
    var selectedAnchor = $("#TagSearchResults a.selected");
    var position = anchors.index(selectedAnchor);

        if (event.keyCode == "13" && anchors.length > 0) {
            FRAMEWORK.AddUpdateInterventionTags(selectedAnchor.attr("id").split("-")[1] + "|" + selectedAnchor.text(), "add");
        }
        if (event.keyCode == "13" && anchors.length == 0 && $("#txtTagSearch").val() != "Start typing to search Tags") {
            FRAMEWORK.AddNewTag($("#txtTagSearch").val());
        }
        else if (event.keyCode == "38") {

            if (position > 0) {
                canClose = false;
                selectedAnchor.removeClass("selected");
                var newSelectedAnchor = $(anchors.get(position - 1));
                newSelectedAnchor.addClass("selected");
                $("#txtTagSearch").val(newSelectedAnchor.text());
            }
        }
        else if (event.keyCode == "40") {

            if (position <= anchors.length) {
                canClose = false;
                selectedAnchor.removeClass("selected");
                var newSelectedAnchor = $(anchors.get(position + 1));
                newSelectedAnchor.addClass("selected");
                $("#txtTagSearch").val(newSelectedAnchor.text());
                //newSelectedAnchor.focus();
            }
        }
    };

    this.AjaxSearch = function (text) {
        var div = $("#TagSearchResults");
        var anchors = $("#TagSearchResults a");
        var selectedAnchor = $("#TagSearchResults a.selected");
        var position = anchors.index(selectedAnchor);

        if (event.keyCode == "13") {
            FRAMEWORK.TagNavigation(event);
        }
        else if (event.keyCode == "38") {
            FRAMEWORK.TagNavigation(event);
        }
        else if (event.keyCode == "40") {
            FRAMEWORK.TagNavigation(event);
        }
        else if (text.length >= 3) {
            FRAMEWORK.RenderSearchResults(text);
        }
        else {
            $("#TagSearchResults").html("");
            $("#TagSearchResults").hide();
        }
    };

正如您在TagNavigation函数(键代码40)中所看到的,我尝试将焦点设置在活动元素上,但仍然没有成功。

请帮助。

1 个答案:

答案 0 :(得分:1)

您需要检查天气,新选择的元素的Y值高于包含div的底部。如果是这样,则将div按新元素的高度滚动。将您的'if(event.keyCode ==“40”)'语句更改为以下内容:

this.TagNavigation = function (event) {
        var div = $("#TagSearchResults");
        var anchors = $("#TagSearchResults a");
        var selectedAnchor = $("#TagSearchResults a.selected");
        var position = anchors.index(selectedAnchor);

        if (event.keyCode == "13" && anchors.length > 0) {
            FRAMEWORK.AddUpdateInterventionTags(selectedAnchor.attr("id").split("-")[1] + "|" + selectedAnchor.text(), "add");
        }
        if (event.keyCode == "13" && anchors.length == 0 && $("#txtTagSearch").val() != "Start typing to search Tags") {
            FRAMEWORK.AddNewTag($("#txtTagSearch").val());
        }
        else if (event.keyCode == "38") {

            if (position > 0) {
                canClose = false;
                selectedAnchor.removeClass("selected");
                var newSelectedAnchor = $(anchors.get(position - 1));
                newSelectedAnchor.addClass("selected");
                $("#txtTagSearch").val(newSelectedAnchor.text());
                var newSelectedAnchorPosistion = newSelectedAnchor.offset();
                var divPosition = div.offset();
                divPosition = divPosition.top;
                if (newSelectedAnchorPosistion.top + 1 > divPosition) {
                    var newPos = div.scrollTop() - newSelectedAnchor.outerHeight();
                    div.scrollTop(newPos);
                }
            }
        }
        else if (event.keyCode == "40") {
            if (position < anchors.length - 1) {
                canClose = false;
                selectedAnchor.removeClass("selected");
                var newSelectedAnchor = $(anchors.get(position + 1));
                newSelectedAnchor.addClass("selected");
                $("#txtTagSearch").val(newSelectedAnchor.text());
                var newSelectedAnchorPosistion = newSelectedAnchor.offset();
                var divPosition = div.offset();
                divPosition = divPosition.top + div.outerHeight();
                if (newSelectedAnchorPosistion.top + 1 >= divPosition) {
                    var newPos = div.scrollTop() + newSelectedAnchor.outerHeight();
                    div.scrollTop(newPos);
                }
            }
        }
    };