Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何跨浏览器使用relatedTarget?_Javascript_Google Chrome_Internet Explorer_Firefox_Blur - Fatal编程技术网

Javascript 如何跨浏览器使用relatedTarget?

Javascript 如何跨浏览器使用relatedTarget?,javascript,google-chrome,internet-explorer,firefox,blur,Javascript,Google Chrome,Internet Explorer,Firefox,Blur,Firefox支持relatedTarget用于focus/blur但不支持focusin/focusout事件 IE仅支持focusin/focusout事件,而不支持focus/blur事件 Chrome支持这两种功能 有没有跨浏览器使用relatedTarget的方法 下面是一个blur示例(使用Chrome和FF,但不使用IE): 这里有一个聚焦于示例(使用Chrome和IE,但不使用FF): 编辑 我最终使用了浏览器检测。所以我用 var eventType; if (naviga

Firefox支持
relatedTarget
用于
focus
/
blur
但不支持
focusin
/
focusout
事件

IE仅支持
focusin
/
focusout
事件,而不支持
focus
/
blur
事件

Chrome支持这两种功能

有没有跨浏览器使用
relatedTarget
的方法

下面是一个
blur
示例(使用Chrome和FF,但不使用IE):

这里有一个
聚焦于
示例(使用Chrome和IE,但不使用FF):

编辑

我最终使用了浏览器检测。所以我用

var eventType;
if (navigator.userAgent.toLowerCase().indexOf('firefox') > -1) {
    eventType = 'blur';
} else {
    eventType = 'focusout';
}
然后
…addEventListener(eventType,…)

(作为答案发布,以便我可以使用代码示例。尽管这更多的是作为一个注释。)

这个问题有很多重复的答案,只是没有100%的答案。再说一次,一旦你使用大型/复杂的应用程序,这个问题就不会经常出现。如果我需要用户操作的东西的顺序,我只需要将它保存为一个变量

因此,我的代码只需反转逻辑,并使用“焦点”而不是模糊,如果您需要它来处理第一个焦点的话。 如果用户没有单击另一个输入事件,也需要发生模糊事件,我还将焦点事件处理程序绑定到页面上的任何单击事件。 这种策略将是交叉的,它只是不美观和/或效率

这并不完美,因为它会在第一次单击后触发两个事件,但这很容易解决。但是随着playign与代码的结合,应该可以找到模拟所需行为的事件组合。(因此,我需要更多关于console元素何时应该写入id或不写入anm实现的详细信息。)


var currentElement,
inputOne=document.getElementById('one'),
InputWO=document.getElementById('two'),
inputConsole=document.getElementById('console'),
focusHandler=函数(事件){
if(currentElement)inputConsole.value+=currentElement.id;
currentElement=event.target;
};
inputOne.addEventListener('focus',focusHandler);
Inputwo.addEventListener('focus',focusHandler);
//“聚焦非输入元素”的可选处理程序,即模糊上一个单击的元素。
文件。添加的文件列表器(“点击”,焦点处理器);

我尝试了一些手动创建事件的方法,然后检查是否可以从中获取相关目标,但根据我目前的知识,我无法在IE和chrome中进行任何操作。所以,要么在window.navigator中检测firefox,要么像我一直在尝试的那样尝试使事件功能检测工作,要么在最坏的情况下,在其中存储对上次聚焦目标的引用的变量。@Shilly感谢您的努力。我希望我能想出一些办法。奇怪的是,似乎没有其他人遇到过这个问题。谢谢。与此同时,我使用了你在其他地方提到的浏览器检测方法(参见我的文章的编辑)。
<!DOCTYPE html>
<html lang="en">
<head>
    <title></title>
</head>
<body>
    <input id="one" value="focus me first">
    <input id="two" value="focus me next">
    <input id="console">
    <script>
    var currentElement,
        inputOne = document.getElementById('one'),
        inputTwo = document.getElementById('two'),
        inputConsole = document.getElementById('console'),
        focusHandler = function( event ) {
            if (currentElement) inputConsole.value += currentElement.id;
            currentElement = event.target;
        };
    inputOne.addEventListener('focus', focusHandler);
    inputTwo.addEventListener('focus', focusHandler);
    // optional handler for 'focus non-input elements', aka blur the prev clicked element.
    document.addEventListener('click', focusHandler);
    </script>
</body>
</html>