如何与onblur一起正确处理onclick事件 [英] how to properly handle an onclick event in conjunction with an onblur

查看:180
本文介绍了如何与onblur一起正确处理onclick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下帖子涉及上一个我问过的问题这里

The following post relates to a previous question I asked here.

虽然这两个问题是独立的,但它们与我实施的相同功能有关 - 预测文本。

Although the 2 problems are independent they do relate to the same feature that I implementing - predictive text.

我遇到的问题与如何调用2个事件 onblur onclick 有关。

The problem I am running into has to do with how the 2 events onblur and onclick are called.

在用户在文本框中输入一些字符并决定他们想要点击建议而不是完成输入整个单词之后,就会出现这种情况。

The situation occurs after the user has typed some characters into the text box and decide that they would like to instead click on a suggestion instead of finishing typing the entire word.

我有一个 onlick 处理程序附加到每个建议。

I have an onlick handler attached to each suggestion.

但是,我的文本框附有一个 onblur 处理程序。

However, I also have an onblur handler attached to my textbox.

问题在于onblur处理程序应该关闭建议框并销毁其内容。

The problem is that the onblur handler is supposed to CLOSE the suggestions box and destroy its contents.

onlick手ler需要内容才能复制点击的div的值并将其复制到文本框中。

The onlick handler needs the contents in order to copy the value of the clicked div and copy it into the textbox.

因此,进退两难。如果已被onblur处理程序销毁,我无法复制任何内容。

Hence, the dilemma. I can't copy anything if its already been destroyed by the onblur handler.

如何捕获 onblur 事件,但同时也弄清楚用户是否通过点击其中一条建议触发了onblur?

How do I trap an onblur event, but also at the same time figure out if the onblur was triggered by the user "clicking" on one of the suggestions?

希望我的问题有道理。

推荐答案

我尝试了很多不同的东西,包括将我的文本输入字段和我的autoFill div包装在父div中并设置onblur on父母 - 但没有任何效果。显然你不能在div上分配一个onblur事件(因为它无法获得焦点开始)。

I tried a number of different things including wrapping both my text entry field and my autoFill div in a parent div and setting the onblur on the parent - but nothing worked. Apparently you cannot assign an onblur event on a div (since it cannot get focus to begin with).

答案是在 onblur Seth建议的事件。

The answer was to set a Timeout on the onblur event as Seth has suggested.

onblur="setTimeout(function() {closeSuggestionBox();}, 100);"

我在帖子 here

这篇关于如何与onblur一起正确处理onclick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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