HTML输入字段在单击时不会获得焦点 [英] HTML input fields does not get focus when clicked

查看:173
本文介绍了HTML输入字段在单击时不会获得焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,我不知道究竟是什么造成这种行为。我无法访问我的HTML表单上的输入字段和 textarea

I have a problem and I can't figure out what exactly is causing this behavior. I cannot access my input fields and textareas on my HTML form.

不幸的是,JS,HTML和CSS是非常大,所以我不能真正地张贴在这里。

Unfortunately, the JS, HTML and CSS are very large, so I can't really post it all here.

任何人都可以告诉我在调试这个奇怪行为时应该查找什么。

Can anybody tell me what to look for when debugging this strange behavior?

UPDATE

UPDATE

如果我将光标移动到输入字段,我可以看到文本光标,它的字段没有得到焦点。我可以通过按 Tab 键访问该字段,如果我右键点击它,然后点击字段我也得到它的焦点。

If I move the cursor over the input field I can see the text cursor, but when I click it the field does not get the focus. I can access the field via pressing the Tab key and if I right click on it and then click on the field I also get the focus for it.

...并且不,它们没有 disabled readonly 属性; - )

...and nope, they don't have the disabled or readonly attributes ;-)

推荐答案


当我点击它的字段没有得到焦点。我可以通过按tab键访问该字段

when i click it the field does not get the focus. i can access the field via pressing the "tab-key"

这听起来像您取消了 mousedown 事件。在您的HTML和JS中搜索 处理程序,并寻找一行代码。

It sounds like you've cancelled the default action for the mousedown event. Search through your HTML and JS for onmousedown handlers and look for a line that reads.

return false;

此行可能会通过点击停止您的聚焦。

This line may be stopping you from focusing by clicking.

Re:你的意见,我假设你不能编辑添加这个处理程序的代码?如果可以,最简单的解决方案是删除 return false;

Re: your comment, I'm assuming you can't edit the code that adds this handler? If you can, the simplest solution is to just remove the return false; statement.


有没有办法只是添加功能到事件触发器不覆盖它?

is there a way to just add functionality to the event-trigger by not overwriting it?

这取决于处理程序如何附件。如果使用传统的注册方法 element.onmousedown ,那么您可以为其创建一个包装器:

That depends on how the handler is attached. If it's attached using the traditional registration method, e.g. element.onmousedown, then you could create a wrapper for it:

var oldFunc = element.onmousedown;
element.onmousedown = function (evt) {
    oldFunc.call(this, evt || window.event);
}

由于此包装程序不会返回 false ,它不会取消元素的默认动作(聚焦)。如果您使用高级注册方法(例如 addEventListener attachEvent )附加事件,那么您只能使用函数名称/引用删除事件处理程序,功能类似上面。如果它是一个被添加的匿名函数,并且你不能得到它的引用,那么唯一的解决方案将是附加另一个事件处理程序,并使用 element.focus()方法手动聚焦元素。

Since this "wrapper" doesn't return false, it will not cancel the default action (focusing) for the element. If your event is attached using an advanced registration method, such as addEventListener or attachEvent then you could only remove the event handler using the function name/reference and reattach it with a wrapped function similar to the above. If it's an anonymous function that's added and you can't get a reference to it, then the only solution would be to attach another event handler and focus the element manually using the element.focus() method.

这篇关于HTML输入字段在单击时不会获得焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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