Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.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 Internet Explorer 11错误地触发;输入“;立即事件_Javascript - Fatal编程技术网

Javascript Internet Explorer 11错误地触发;输入“;立即事件

Javascript Internet Explorer 11错误地触发;输入“;立即事件,javascript,Javascript,我在调试一个奇怪的JavaScript问题,并注意到我认为IE11中可能存在的浏览器错误。我在StackOverflow上发布这篇文章,作为一种精神检查——也许是为了确保其他使用IE11的人可以复制这篇文章 问题在于,当隐藏输入的值包含HTML实体时,页面加载时立即在元素上触发input事件 请看这里的这个单独复制: 页面的源代码: <!doctype html> <html> <head> <script> document.addE

我在调试一个奇怪的JavaScript问题,并注意到我认为IE11中可能存在的浏览器错误。我在StackOverflow上发布这篇文章,作为一种精神检查——也许是为了确保其他使用IE11的人可以复制这篇文章

问题在于,当隐藏输入的值包含HTML实体时,页面加载时立即在元素上触发
input
事件

请看这里的这个单独复制:

页面的源代码:

<!doctype html>
<html>
<head>
  <script>
    document.addEventListener('DOMContentLoaded', function(){
      document.getElementById('foo').addEventListener('input', function(){
        alert('Crap!');
      });
    });
  </script>
</head>
<body>
  In IE11, refresh the page. You will see an alert, whereas in other browsers, you will not see an alert.

  <form>
    <input id="foo" type="hidden" name="utf8" value="&#x2713;" />
  </form>
</body>
</html>

document.addEventListener('DOMContentLoaded',function(){
document.getElementById('foo').addEventListener('input',function(){
警惕(‘废话’);
});
});
在IE11中,刷新页面。您将看到警报,而在其他浏览器中,您将看不到警报。

有人能帮我确认这是否是一个浏览器错误吗?或者我做错了什么?

对于任何可能遇到此问题的人,您可以使用此“类”作为onInput事件的替代方案

const getFieldWatcherInstance = (function(watchedElement, onElementValueChange){
        let intervalReference = null;
        let lastValue = null;
        if(!watchedElement instanceof Element) return new Error('watchedElement is not an HTML Element');
        if(typeof onElementValueChange !== 'function') return new Error('onElementValueChange is not a function');
        return {
            toggleFieldWatching : function(){
                if(intervalReference){
                    clearInterval(intervalReference);
                    intervalReference = null;
                }
                else{
                    intervalReference = setInterval(function(){
                        const currentValue = watchedElement.value;
                        if(lastValue !== currentValue){
                            onElementValueChange(currentValue);
                            lastValue = currentValue;
                        }    
                    }, 100);   
                }    
            }
        };    
    });
设置如下:

const myInputChangeWatcher = getFieldWatcherInstance(<**insert real element reference here**>, function(newValue){
console.log('The new value is: ' + newValue);
});
const myInputChangeWatcher=getFieldWatcherInstance(,函数(newValue){
log('新值为:'+newValue);
});

在输入的onfocus和onblur事件中调用myInputChangeWatcher.toggleFieldWatching()

关于此问题存在一个公开的错误:此问题在IE10中持续存在+