4

我有一个可搜索的文本框,它用搜索结果填充 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)中看到的那样,我尝试将焦点设置在活动元素上,但仍然没有成功。

请提供任何帮助。

4

1 回答 1

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);
                }
            }
        }
    };
于 2012-08-17T07:37:30.723 回答