文本字段不集中,除非窗口是焦点 [英] Text field won't focus unless window is in focus

查看:106
本文介绍了文本字段不集中,除非窗口是焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,可以自动输入用户信息Greasemonkey的脚本。但是,可以提交表单前,田野都成为关注焦点(我认为他们使用AngularJS验证有效的名称/电子邮件已输入)。所以在我的code我做的:

 的document.getElementById('名')值=此名。; //在字段中输入名称
。的document.getElementById('名')关注(); //重点领域

这工作得很好,但只有当窗口是活动的。如果窗口不是活动的,它就会进入名称,但该领域不会来进行对焦(所以它不能被提交)。有没有可以做,以解决这一问题的任何修改?

编辑:这是我很难提供一个例子,因为code必须从一个窗口,处于非活动状态运行,但这里是code为输入域

 <输入ID =名称字段[名]NAME =名NG-模式=nameField.form.nameNG-模式=nameRegex()占位符= 李四所需=的风格=WIDTH:246px类型=文本级=NG-有效图案NG-脏NG-有效NG-有效要求的>


解决方案

AngularJS在很大程度上AJAX驱动的,所以它可能是一个时间的事情。 JS,尤其是定时器,减慢对不具有焦点页面。

因此​​,它可以是仅仅使用AJAX感知技术将是足够的。 EG:

  // == == UserScript
// @name _Focus名字输入
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant GM_addStyle
// == / UserScript ==
/ * - 需要@grant指令来解决设计变更
    通用汽车1.0引入的。它恢复沙箱。
* /
waitForKeyElements(#NAME,focusInput,真正的);功能focusInput(jNode){
    jNode [0]。重点();
}


请注意,这个问题code列出:<输入ID =名称字段[名]...
但对于ID搜索名称。结果
如果这不是一个错误,这是一个迹象,表明AngularJS正在改写的HTML(我还没学到了很多AngularJS) - 这将是进一步的证据,原来的剧本曾在一切都因为赢得一场比赛的条件。即,不可靠的。

I have a Greasemonkey script that automates inputting user info. But before the form can be submitted, the fields have to be in focus (I think they use AngularJS to verify that a valid name/email has been entered). So in my code I do:

document.getElementById('name').value = "Name here"; //Enter name in field
document.getElementById('name').focus(); //Focus on the field

This works fine but only when the window is active. If the window is not active, it will enter the name but the field will not come to focus (so it cannot be submitted). Is there any modification that can be done to fix this?

Edit: It is hard for me to provide an example because the code has to be run from a window that is not active but here is the code for the input field.

<input id="namefield[name]" name="name" ng-model="nameField.form.name" ng-pattern="nameRegex()" placeholder="John Doe" required="" style="width: 246px" type="text" class="ng-valid-pattern ng-dirty ng-valid ng-valid-required">

解决方案

AngularJS is heavily AJAX driven, so it is probably a timing thing. JS, especially timers, is slowed down on pages that don't have focus.

So, it may be that just using AJAX-aware techniques will be enough. EG:

// ==UserScript==
// @name     _Focus the name input
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
waitForKeyElements ("#name", focusInput, true);

function focusInput (jNode) {
    jNode[0].focus ();
}


Note that the question code lists: <input id="namefield[name]"... but searches for id name.
If this isn't an error, it is a sign that AngularJS is rewriting the HTML (I've not yet learned much AngularJS) -- which would be further evidence that that the original script worked at all was due to winning a race condition. That is, not reliable at all.

这篇关于文本字段不集中,除非窗口是焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆