0

我有两页。第一页中的下一个按钮带来第二页。第二页的焦点总是向下移动。所以我需要使用滚动条将光标带到顶部。我想把焦点带到顶部。我的 focus-handler.js 如下:

     var lastFocusedControlId = "";

function focusHandler(e) {
    document.activeElement = e.originalTarget;
}

function appInit() {
    if (typeof (window.addEventListener) !== "undefined") {
        window.addEventListener("focus", focusHandler, true);
    }

    setFirstControl()

    Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler);
    Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler);
}

function pageLoadingHandler(sender, args) {
    lastFocusedControlId = typeof (document.activeElement) === "undefined"
        ? "" : document.activeElement.id;
}

function focusControl(targetControl) {
    if (Sys.Browser.agent === Sys.Browser.InternetExplorer) {
        var focusTarget = targetControl;
        if (focusTarget && (typeof (focusTarget.contentEditable) !== "undefined")) {
            oldContentEditableSetting = focusTarget.contentEditable;
            focusTarget.contentEditable = false;
        }
        else {
            focusTarget = null;
        }

        try {
            targetControl.focus();
            if (focusTarget) {
                focusTarget.contentEditable = oldContentEditableSetting;
            }
        }
        catch (err) { }
    }
    else {
        targetControl.focus();
    }
}

function pageLoadedHandler(sender, args) {
    if (typeof (lastFocusedControlId) !== "undefined" && lastFocusedControlId != "") {
        var newFocused = $get(lastFocusedControlId);
        if (newFocused) {
            focusControl(newFocused);
        }
    }
}

function setFirstControl() {
    var bFound = false;

    // for each form
    for (f = 0; f < document.forms.length; f++) {
        // for each element in each form
        for (i = 0; i < document.forms[f].length; i++) {
            // if it's not a hidden element
            if (document.forms[f][i].type != "hidden") {
                // and it's not disabled
                if (document.forms[f][i].disabled != true) {
                    try {
                        // set the focus to it
                        document.forms[f][i].focus();
                        var bFound = true;
                    }
                    catch (er) {
                    }
                }
            }
            // if found in this element, stop looking
            if (bFound == true)
                break;
        }
        // if found in this form, stop looking
        if (bFound == true)
            break;
    }
}

Sys.Application.add_init(appInit);
4

0 回答 0