gpt4 book ai didi

jquery - 为什么 jqLit​​e 在我的代码中第一次没有聚焦?

转载 作者:行者123 更新时间:2023-12-01 05:42:28 25 4
gpt4 key购买 nike

我有一个指令来自动聚焦下一个输入,如下所示。该项目是 Ionic 的 angularJs。它在另一个项目中工作(这是基于音隙的 - 不是 ionic 的),但在这个 ionic 项目中它没有发挥作用。标记是相同的。

(function() {
'use strict';
angular.module('appname.directives')
.directive('focus', [function() {
return {
restrict: 'A',
link: function($scope,elem) {

//after character keypress the focus switches to the next input
elem.bind('keyup', function(e) {

if (e.keyCode >= 48 && e.keyCode <= 90 || e.keyCode >= 96 && e.keyCode <= 105) {

if (elem[0].value !== "") {
if (elem.nextAll('input') && elem.nextAll('input').length > 0) {
elem.nextAll('input').first().focus();
} else {
elem.parent().parent().nextAll(".module__inner").first().find('input').first().focus();
}
}
}
});
}
};
}]);
})();

该 html 看起来有点像下面这样:

    <div class="input-holder">
<input data-order="1" focus />
<input data-order="2" focus />
<input data-order="3" focus />
</div>

奇怪的是,在调试时我可以看到代码执行该行“elem.nextAll('input').first().focus();”

这确实会按下 native 键盘,但不会集中下一个输入。奇怪的是,当我“制表符”到下一个输入时,指令就会起作用,我可以返回到第一个输入,它会按预期自动制表符。

就绑定(bind)指令而言,我可以调试并看到它正在触发,所以它不可能是与此相关的问题。 HTML 是静态的,因此不存在事件委托(delegate)问题。

我对此感到困惑,我唯一不断回想起的事实是最初的 .focus() 似乎并没有真正聚焦,而每次它都会聚焦。

我尝试将其包装在 $timeout 和 $scope.$apply() 中,但似乎不起作用。

有人知道我可以尝试什么吗?

<小时/>

编辑

<小时/>

奇怪的是,当我的 Chrome 开发工具未打开时,它会按预期工作。它也不适用于设备。我看不出除了执行周期较慢之外还有什么区别?

最佳答案

我的设备上也有类似的问题。

如果您使用 Ionic + AngularJS,则需要在 config.xml 中添加 Ionic 键盘插件和这些首选项

<preference name="KeyboardDisplayRequiresUserAction" value="false" />

<feature name="Keyboard">
<param name="ios-package" value="IonicKeyboard" onload="true" />
</feature>

KeyboardDisplayRequiresUserAction 首选项是阻碍焦点功能的首选项。

关于jquery - 为什么 jqLit​​e 在我的代码中第一次没有聚焦?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29923573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com