当用户点击输入框时,将执行两个连续的事件:
我正在寻找一种干净的解决方案,可以帮助我实现此功能(不需要任何超时或奇怪的黑客技巧)。 HTML:
focus
和click
。
focus
总是首先执行并显示通知。但是,紧接在focus
之后立即运行的click
会隐藏通知。只有在输入框未聚焦且两个事件连续执行时才会出现此问题。我正在寻找一种干净的解决方案,可以帮助我实现此功能(不需要任何超时或奇怪的黑客技巧)。 HTML:
<label for="example">Example input: </label>
<input type="text" id="example" name="example" />
<p id="notice" class="hide">This text could show when focus, hide when blur and toggle show/hide when click.</p>
JavaScript:
$('#example').on('focus', _onFocus)
.on('blur', _onBlur)
.on('click', _onClick);
function _onFocus(e) {
console.log('focus');
e.preventDefault();
e.stopPropagation();
e.stopImmediatePropagation();
$('#notice').removeClass('hide');
}
function _onClick(e) {
console.log('click');
$('#notice').toggleClass('hide');
}
function _onBlur(e) {
console.log('blur');
$('#notice').addClass('hide');
}
UPDATED Fiddle is here: