asp.net下一页的重点是始终向底部移动

时间:2012-10-30 19:31:30

标签: javascript asp.net

我有两页。第一页中的下一个按钮带来第二页。第二页的焦点总是向下移动。所以我需要使用滚动条带上光标顶部。我想将焦点放在首位。我的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);

0 个答案:

没有答案