Table of Contents
回复讨论(解决方案)
Home Web Front-end HTML Tutorial 如何在blur或focusout事件里得到即将得到焦点的元素?另外这两个事件有什么区别?_html/css_WEB-ITnose

如何在blur或focusout事件里得到即将得到焦点的元素?另外这两个事件有什么区别?_html/css_WEB-ITnose

Jun 21, 2016 am 09:43 AM

本帖最后由 Javkburd 于 2013-05-11 23:21:32 编辑

    我实现了一个类似搜索栏的功能,就是在输入框里输入,然后显示类似下拉列表来匹配已输入字符串(其实就是动态生成一个一列的table),其它的一些细节都处理好了,而且对于IE,Chrome,Safari和Firefox都做到了兼容。但是就是想在输入框失去焦点时让table消失,但是问题是”点击table本身时输入框也会失去焦点,但是此时点击是为了让table某一行匹配项显示到输入框内然后再消失,而不是立即消失”。此时在focusout里捕获的得到焦点的元素是body而不是table,应该是事件冒泡到body了。但是此时table还没有得到焦点而只是即将得到焦点,要是能得到实际得到焦点的元素,就能够判断“如果是table,那么点击某行后显示匹配内容到输入框内然后再消失,如果不是table就直接消失”。一种笨方法是在其它所有非table元素的focus事件内让table消失,但是这样太麻烦了。最后我想知道focusout和blur的区别。页面运行效果如下图所示(没有显示页面其它元素):

回复讨论(解决方案)

笨办法是

输入框 失去焦点后
settimeout 一段时间后 弹出的列表框 消失  这样可以给你点一下的机会

setTimeout延时隐藏table,再写table的mouseenter事件,清除定时器,mouseleave事件隐藏table

var timer;
$(input).blur(function(){
    timer = setTimeout(function(){
        $(table).hide();
    },200);
});
$(table).mouseenter(function(){
    if(timer) clearTimeout(timer);
});
$(table).mouseleave(function(){
    $(table).hide();
});
能看明白?

setTimeout延时隐藏table,再写table的mouseenter事件,清除定时器,mouseleave事件隐藏table

var timer;
$(input).blur(function(){
    timer = setTimeout(function(){
        $(table).hide();
    },200);
});
$(table).mouseenter(function(){
    if(timer) clearTimeout(timer);
});
$(table).mouseleave(function(){
    $(table).hide();
});
能看明白?
你这话说的,有什么不明白的? 问题已经解决。我还想问的是blur和focusout有哪些区别,网上找不到好的资源。

setTimeout延时隐藏table,再写table的mouseenter事件,清除定时器,mouseleave事件隐藏table

var timer;
$(input).blur(function(){
    timer = setTimeout(function(){
        $(table).hide();
    },200);
});
$(table).mouseenter(function(){
    if(timer) clearTimeout(timer);
});
$(table).mouseleave(function(){
    $(table).hide();
});
能看明白?
只是有一点我不懂,就是我在table的td内添加函数

td.onclick = function ()        {            if(timer)  clearTimeout(timer);            source.value = this.innerHTML;  //注意TextBox的innerHTML为空,因为text位于元素内部            $("#" + tableId).remove();        }
Copy after login
Copy after login
Copy after login

虽然也在TextBox的blur函数内设置了延时定时器
$("#txtEmail").blur(function(event)            {                timer = setTimeout(function () { $("#tableEmail").remove() }, 200);   //异步,立即返回                //设置定时器延时让table得到焦点并处理后再消失,而且延时不能太短也不能太长            })
Copy after login
Copy after login
Copy after login

但是如果不在td.onclick内不写$("#" + tableId).remove(),table并不会消失,也就是并没有执行定时器内的延时函数,难道延时内只要有执行过程就会阻塞执行定时器内的延时函数导致其不执行吗?


setTimeout延时隐藏table,再写table的mouseenter事件,清除定时器,mouseleave事件隐藏table

var timer;
$(input).blur(function(){
timer = setTimeout(function(){
$(table).hide();
},200);
});
$(table).mouseenter(function(){
if(timer) clearTimeout(timer);
});
$(table).mouseleave(function(){
$(table).hide();
});
能看明白?
只是有一点我不懂,就是我在table的td内添加函数

td.onclick = function ()        {            if(timer)  clearTimeout(timer);            source.value = this.innerHTML;  //注意TextBox的innerHTML为空,因为text位于元素内部            $("#" + tableId).remove();        }
Copy after login
Copy after login
Copy after login

虽然也在TextBox的blur函数内设置了延时定时器
$("#txtEmail").blur(function(event)            {                timer = setTimeout(function () { $("#tableEmail").remove() }, 200);   //异步,立即返回                //设置定时器延时让table得到焦点并处理后再消失,而且延时不能太短也不能太长            })
Copy after login
Copy after login
Copy after login

但是如果不在td.onclick内不写$("#" + tableId).remove(),table并不会消失,也就是并没有执行定时器内的延时函数,难道延时内只要有执行过程就会阻塞执行定时器内的延时函数导致其不执行吗?

因为先执行了input的blur事件,再执行td的onclick事件,于是在onclick的时候,先clear掉了timer,所以就不会执行setTimeout里面的函数了。

至于你问的fousout跟blur事件有什么区别,我没接触过fousout,我估计fousout事件应该会比blur多一个属性targetElement||toElement,类似mouseout,可以获取fousout后哪个对象获得焦点。



setTimeout延时隐藏table,再写table的mouseenter事件,清除定时器,mouseleave事件隐藏table

var timer;
$(input).blur(function(){
timer = setTimeout(function(){
$(table).hide();
},200);
});
$(table).mouseenter(function(){
if(timer) clearTimeout(timer);
});
$(table).mouseleave(function(){
$(table).hide();
});
能看明白?
只是有一点我不懂,就是我在table的td内添加函数

td.onclick = function ()        {            if(timer)  clearTimeout(timer);            source.value = this.innerHTML;  //注意TextBox的innerHTML为空,因为text位于元素内部            $("#" + tableId).remove();        }
Copy after login
Copy after login
Copy after login

虽然也在TextBox的blur函数内设置了延时定时器
$("#txtEmail").blur(function(event)            {                timer = setTimeout(function () { $("#tableEmail").remove() }, 200);   //异步,立即返回                //设置定时器延时让table得到焦点并处理后再消失,而且延时不能太短也不能太长            })
Copy after login
Copy after login
Copy after login

但是如果不在td.onclick内不写$("#" + tableId).remove(),table并不会消失,也就是并没有执行定时器内的延时函数,难道延时内只要有执行过程就会阻塞执行定时器内的延时函数导致其不执行吗?

因为先执行了input的blur事件,再执行td的onclick事件,于是在onclick的时候,先clear掉了timer,所以就不会执行setTimeout里面的函数了。

至于你问的fousout跟blur事件有什么区别,我没接触过fousout,我估计fousout事件应该会比blur多一个属性targetElement||toElement,类似mouseout,可以获取fousout后哪个对象获得焦点。
我所知道的有srcElement,fromElement,toElement,target和relatedTarget,但是好像都不能得到即将得到焦点的元素,不知道他们有什么特殊含义。

srcElement|| target 当前目标元素
fromElement || relatedTarget  从何元素进入当前元素
toElement || relatedTarget   从当前元素进入到何元素

简单点说就是:
我是谁
我从何而来
我去往何方

srcElement|| target 当前目标元素
fromElement || relatedTarget  从何元素进入当前元素
toElement || relatedTarget   从当前元素进入到何元素

简单点说就是:
我是谁
我从何而来
我去往何方
srcElement和target属性在blur或focusout中可用,其它三个不行;但是在onmouseover和onmouseout中所有属性都可用。所以确实不能在此获得即将得到焦点的元素了,只能采用延时了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Difficulty in updating caching of official account web pages: How to avoid the old cache affecting the user experience after version update? Mar 04, 2025 pm 12:32 PM

The official account web page update cache, this thing is simple and simple, and it is complicated enough to drink a pot of it. You worked hard to update the official account article, but the user still opened the old version. Who can bear the taste? In this article, let’s take a look at the twists and turns behind this and how to solve this problem gracefully. After reading it, you can easily deal with various caching problems, allowing your users to always experience the freshest content. Let’s talk about the basics first. To put it bluntly, in order to improve access speed, the browser or server stores some static resources (such as pictures, CSS, JS) or page content. Next time you access it, you can directly retrieve it from the cache without having to download it again, and it is naturally fast. But this thing is also a double-edged sword. The new version is online,

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

How to efficiently add stroke effects to PNG images on web pages? How to efficiently add stroke effects to PNG images on web pages? Mar 04, 2025 pm 02:39 PM

This article demonstrates efficient PNG border addition to webpages using CSS. It argues that CSS offers superior performance compared to JavaScript or libraries, detailing how to adjust border width, style, and color for subtle or prominent effect

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

See all articles