首页 > 解决方案 > 如何从标签顺序中删除 Bing 地图(和所有子控件)

问题描述

Bing 地图控件 (Microsoft.Maps.Map) 是否提供 API 用于从选项卡顺序中删除自身(及其子控件)?

或者,是否有适当的回调(渲染后),一旦地图本身呈现,我可以使用递归地在所有元素上设置 tabIndex?

注意:我已经看到在构建时有一些选项可以指定是否呈现地图类型选择器/定位我按钮/缩放按钮。但是让我们假设我们想要渲染这些控件,只需将它们从 Tab 键顺序中删除。

标签: javascriptbing-maps

解决方案


我设法使用 MutationObserver 来实现它:

var mapElem = document.getElementById("map");

let deferredClearTabIndexEvent: number | undefined;

if (mapElem !== null) {
    var config = { attributes: true, childList: true, subtree: true };
    var callback = () => {
        if (deferredClearTabIndexEvent === undefined) {
            deferredClearTabIndexEvent = window.setTimeout(() => {
                var elements = mapElem!.querySelectorAll("a, button, input, select, textarea, [tabindex]")
                for (var i=0; i<elements.length; i++) {
                    elements[i].setAttribute("tabIndex", "-1");
                }

                deferredClearTabIndexEvent = undefined;
            }, 0);
        }
    };

    var observer = new MutationObserver(callback);
    observer.observe(mapElem, config);
}

推荐阅读