简体   繁体   English

单击div时如何保持输入文本焦点不变?

[英]How to keep input text focus still when clicked on div?

Just like title says, I'm trying to keep the input text focus still when click on one div. 就像title所说的那样,我试图在单击一个div时保持输入文本的焦点不变。

Specifically I'm trying something like what happens in Google Livesearch (Not Instant Search) (When you type there, shows a ten items list.) (I can't put an image). 具体来说,我正在尝试类似Google Livesearch(不是即时搜索)中发生的事情(当您在其中键入内容时,会显示十个项目列表。)(我无法放置图片)。

When you click on "Google Search", still keeps input text focus. 当您单击“ Google搜索”时,仍保持输入文本的焦点。

I'm trying but always lose the input text focus. 我正在尝试,但始终会失去输入文本的焦点。

Someone knows how to achieve this?. 有人知道如何实现这一目标吗?

PS: With click I mean the mousedown event. PS:单击表示鼠标按下事件。

Thanks in advance. 提前致谢。

You can try to refocus on your text input when a mousedown event is caught on your division. 当分区上捕获到mousedown事件时,您可以尝试重新关注文本输入。

Here is a quick example using jQuery: 这是一个使用jQuery的简单示例:

$("#my-division").on("mousedown", function (evt) {
    $("#my-input").focus();
});

Check this solution: 检查此解决方案:

First, store the last focused element: 首先,存储最后一个焦点元素:

var lastFormElementFocused = 'my-first-element-id';
$(document).on('focus', '.your-class', function() {
        lastFormElementFocused = $(this).attr('id');
        console.log(lastFormElementFocused);
    });

After that, in your onClick event: 之后,在您的onClick事件中:

$(document).on('click', "div.your-class", function() {
        $("#" + lastFormElementFocused).focus();
        var element = $(document.activeElement);
        // Do with 'element' what you want
    });

Note that you need to have an assigned id in each element you focus. 请注意,您需要在关注的每个元素中分配一个ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM