【问题标题】:Something in the cordova / angular / ionic stack steals my focus科尔多瓦/角度/离子堆栈中的某些东西抢走了我的注意力
【发布时间】:2018-01-23 13:43:22
【问题描述】:

为了提供可访问性,我需要在我的 Cordova 应用程序中进入屏幕时聚焦元素。通过一些技巧,我设法将焦点设置在一个元素上,但该元素会立即失去焦点(通过侦听“模糊”事件来验证)并且丢失发生在 Apples VoiceOver、Androids Talkback 启动或用户可以接受之前任何动作。

我已经尝试将焦点设置包装在 $timeout 中,以强制它在所有内容都呈现后发生,但这根本没有帮助。在我看来,Cordova、angular 1 或 ionic 中的某些东西会在其自身干扰 m 代码时产生一些令人讨厌的焦点魔法。

有没有人经历过类似的事情并找到了解决方法?

【问题讨论】:

  • 什么元素占据了焦点? document.addEventListener('focus', function(e) { console.log(e.target) }, true)
  • body 元素获得焦点,其中还包括 ng-app 属性:<body ng-app="constructionkit" class="grade-a platform-webview platform-cordova platform-android platform-android6 platform-android6_0 platform-ready statusbar-translucent topbar-default modal-open" tabindex="-1"> <!-- Lots of stuff --> </body> 如果两者都尝试:删除并设置 body 上的 tabindex="-1" 属性。没有区别

标签: javascript angularjs cordova ionic-framework accessibility


【解决方案1】:

我对 Cordova-Ionic 使用了以下指令:

app.directive('focusableInput', function($timeout,$log) {
    return {
        restrict: 'A',
        require: 'ngModel',
        scope: {
            focusState: "=",
            onGo: "&"
        },
        link: function(scope, element, attr, ngModel) {

            var moveCursorToEnd = function(el) {
                if (typeof el.selectionStart == "number") {
                    el.selectionStart = el.selectionEnd = el.value.length;
                } else if (typeof el.createTextRange != "undefined") {
                    el.focus();
//                    var range = el.createTextRange();
//                    range.collapse(false);
//                    range.select();
                }
            }

            scope.$watch("focusState", function(state) {
                $log.debug("focusableInput - focus state change: " + state,element);
                if (state) {
                    //cordova.plugins.Keyboard.show();
                    $log.debug("focusableInput - do focus!!!",element);
                    $timeout(function() {
                        element[0].focus();
                        moveCursorToEnd(element[0]);
                    }, 300);

                    //element[0].focus();
                } else {
                    element[0].blur();
                }
            });

            element.on("keydown", function(event) {
                console.log("pressed: " + event.which, event);
                //$log.debug(attr);

                if (event.which == 13) {
                    $timeout(function() {
                        scope.onGo();
                    });
                }
            });



            scope.$watch(
                    function() {
                        return ngModel.$modelValue;
                    },
                    function(newValue) {
                        $log.debug("focus input model changed: ", newValue);

                        if (newValue) {
                            var endChar = newValue[newValue.length - 1];
                            $log.debug("endChar",endChar);

                            if (endChar == " " || endChar == ",") {
                                $timeout(function() {
                                    scope.addItem();
                                });
                            }
                        }
                    }
            );
        }
    };
});

及用法:

<input type="email" placeholder="Email" 
               class="login-custom-input"
               focusable-input 
               focus-state="textAreaFocusState"
               ng-model="meeterAccount.email"
               ng-focus="onSignUpLoginEmailFocus()"
               ng-blur="onSignUpLoginEmailFocusLost()"
               >

focusable-input 指令使用focus-state 属性


希望对你有帮助。

【讨论】:

  • 不幸的是,这对我不起作用。首先:我不想只关注输入字段,也不想关注离子模式视图中的标题。但是,在您提出更笼统的建议后,对我没有帮助。它做了一些事情,但焦点仍然集中在身体上。 (通过 bodyel.addEventListener('focus', function (el) { console.log.('new focus'.document.activeElement)} 验证
猜你喜欢
  • 1970-01-01
  • 2022-07-10
  • 1970-01-01
  • 1970-01-01
  • 2019-06-03
  • 2016-08-30
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多