简体   繁体   English

addEventListener IE11错误

[英]addEventListener IE11 bug

I am working with three.js on IE11 and have a problem with the addEventListener func; 我正在IE11上使用three.js,并且addEventListener函数有问题;

The code inside the function is never being executed: 函数内部的代码永远不会执行:

var fileInput = document.createElement( 'input' ); 
fileInput.type = 'file'; 
fileInput.addEventListener( 'change', function ( event ) { 
        editor.loader.loadFile( fileInput.files[ 0 ] );
} ); 

the browser familiar with the addEventListener func, bet somehow the inside function is not being called when i 'click' fileInput button: 熟悉addEventListener函数的浏览器,打赌我“点击” fileInput按钮时不会调用内部函数:

var importOption = new UI.Panel();
importOption.setClass('option');
importOption.setTextContent('Import');
importOption.onClick(function () {

    fileInput.click();

} );
options.add(importOption);

Any ideas of how can i trigger the 'change' function? 关于如何触发“更改”功能的任何想法? I want it to be triggered by user button press, like it is written in the code. 我希望它由用户按下按钮来触发,就像它写在代码中一样。

This code was taken from the three.js source files, and works just fine on chrome. 这段代码取自three.js源文件,在chrome上可以正常工作。

Thanks in advance. 提前致谢。

I had a similar problem with binding a click event to an html element created in this way. 将click事件绑定到以这种方式创建的html元素时,我遇到了类似的问题。 It didn't work on IE11 in Windows 8, but did in IE11 on Windows 10, Edge and in all other browsers. 它在Windows 8的IE11上不起作用,但在Windows 10,Edge和所有其他浏览器的IE11中都起作用。

My code looked like: 我的代码如下所示:

var trigger = document.createElement( 'div' ); 
fileInput.addEventListener( 'click', function ( e) { 
    console.log("clicked the trigger");
} ); 
trigger.click();

That log wasn't appearing in my console. 该日志没有出现在我的控制台中。

I found that I could resolve the problem by adding the trigger to document.body. 我发现可以通过将触发器添加到document.body来解决问题。

var trigger = document.createElement( 'div' ); 
trigger.style.display = "none";
document.body.appendChild(trigger);
fileInput.addEventListener( 'click', function ( e ) { 
    console.log("clicked the trigger");
} ); 
trigger.click();

Hope that helps! 希望有帮助!

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

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