简体   繁体   English

IE8 event.currentTarget 为 null 或不是对象

[英]IE8 event.currentTarget is null or not an object

There is an incompatibility with IE8 in my Javascript and I am not sure how to resolve it.我的 Javascript 与 IE8 不兼容,我不知道如何解决。 I am getting an error "currentTarget is null or not an object," which is occurring in this snippet of code:我收到一个错误“currentTarget 为 null 或不是对象”,这在这段代码中发生:

(function() {
  var basicTemplate, _timer, _url;
  _timer = null;
  _url = "/search";
  $(document).ready(function() {
    var search, searchCallback, searchFailure;
    $('[data-does=typeahead-rep-search]').on('keyup', function(e) {
      clearTimeout(_timer);
      return _timer = setTimeout(search, 300, e);
    });
    search = function(e) {
      var $query, division, request, target, _data;
      $query = $(e.currentTarget);
      _data = {};
      if (_data.q.length > 2) {
        return request = $.ajax(_url, {
          data: _data,
          success: searchCallback,
          error: searchFailure
        });
      } else {
        return $('#search-results').html("");
      }
    };
    searchCallback = function(data, status, jqXhr) {
      return $('#search-results').html(basicTemplate(data));
    };
    return searchFailure = function(jqXhr, status, errorThrown) {
      console.log('an error has occurred while attempting to search');
      return $('#search-results').html("");
    };
  });

}).call(this);

Does anyone know how I can recode this snippet in order to make it function properly in IE?有谁知道我如何重新编码此代码段以使其在 IE 中正常运行?

Thanks!谢谢!

I found the issue.我发现了这个问题。 The problem is this line:问题是这一行:

setTimeout(search, 300, e);

The easiest fix would be this:最简单的解决方法是:

setTimeout((function(e) { return function() { search(e) }; })(e), 300);

I've wrapped it in a closure to avoid scoping issues.我将它包装在一个闭包中以避免范围界定问题。

From the documentation for setTimeout :setTimeout的文档中:

Note that passing additional parameters to the function in the first syntax does not work in Internet Explorer < 9. If you want to enable this functionality on that browser, you must use a compatibility code (see the Callback arguments paragraph).请注意,以第一种语法向函数传递附加参数在 Internet Explorer < 9 中不起作用。如果要在该浏览器上启用此功能,则必须使用兼容性代码(请参阅回调参数段落)。

I'm just going to copy this all wholesale from the docs, as a reference here.我只是打算从文档中批量复制这些内容,作为参考。

If you need to pass an argument to your callback function, but need it to work in Internet Explorer, which doesn't support sending additional parameters (neither with setTimeout() or setInterval()) you can include this IE-specific compatibility code which will enable the HTML5 standard parameters passage functionality in that browser for both timers just by inserting it at the beginning of your scripts.如果您需要将参数传递给回调函数,但需要它在不支持发送附加参数(无论是 setTimeout() 还是 setInterval())的 Internet Explorer 中工作,您可以包含此特定于 IE 的兼容性代码只需将其插入脚本的开头,即可在该浏览器中为两个计时器启用 HTML5 标准参数传递功能。

 /*\\ |*| |*| IE-specific polyfill which enables the passage of arbitrary arguments to the |*| callback functions of JavaScript timers (HTML5 standard syntax). |*| |*| https://developer.mozilla.org/en-US/docs/DOM/window.setInterval |*| |*| Syntax: |*| var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]); |*| var timeoutID = window.setTimeout(code, delay); |*| var intervalID = window.setInterval(func, delay[, param1, param2, ...]); |*| var intervalID = window.setInterval(code, delay); |*| \\*/ if (document.all && !window.setTimeout.isPolyfill) { var __nativeST__ = window.setTimeout; window.setTimeout = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) { var aArgs = Array.prototype.slice.call(arguments, 2); return __nativeST__(vCallback instanceof Function ? function () { vCallback.apply(null, aArgs); } : vCallback, nDelay); }; window.setTimeout.isPolyfill = true; } if (document.all && !window.setInterval.isPolyfill) { var __nativeSI__ = window.setInterval; window.setInterval = function (vCallback, nDelay /*, argumentToPass1, argumentToPass2, etc. */) { var aArgs = Array.prototype.slice.call(arguments, 2); return __nativeSI__(vCallback instanceof Function ? function () { vCallback.apply(null, aArgs); } : vCallback, nDelay); }; window.setInterval.isPolyfill = true; }

IE Only Fix IE 仅修复

If you want a completely unobtrusive hack for every other mobile or desktop browser, including IE 9 and above, you can either use JavaScript conditional comments:如果您想对所有其他移动或桌面浏览器(包括 IE 9 及更高版本)进行完全不显眼的 hack,您可以使用 JavaScript 条件注释:

 /*@cc_on // conditional IE < 9 only fix @if (@_jscript_version <= 6) (function(f){ window.setTimeout =f(window.setTimeout); window.setInterval =f(window.setInterval); })(function(f){return function(c,t){var a=[].slice.call(arguments,2);return f(function(){c.apply(this,a)},t)}}); @end @*/

Or go for a very clean approach based on the IE HTML conditional feature:或者采用基于 IE HTML 条件功能的非常干净的方法:

 <!--[if lt IE 9]><script> (function(f){ window.setTimeout =f(window.setTimeout); window.setInterval =f(window.setInterval); })(function(f){return function(c,t){ var a=[].slice.call(arguments,2);return f(function(){c.apply(this,a)},t)} }); </script><![endif]-->

Another possibility is to use an anonymous function to call your callback, but this solution is a bit more expensive.另一种可能性是使用匿名函数来调用您的回调,但这种解决方案的成本要高一些。 Example:例子:

 var intervalID = setTimeout(function() { myFunc("one", "two", "three"); }, 1000);

Yet another possibility is to use function's bind.另一种可能性是使用函数的绑定。 Example:例子:

 setTimeout(function(arg1){}.bind(undefined, 10));

Alright, I resolved the issue by modifying the code as seen below.好的,我通过修改代码解决了这个问题,如下所示。 Please let me know if this is not the recommended method of resolving this issue.如果这不是解决此问题的推荐方法,请告诉我。

SOURCE 来源

search = function(e) {
      var $query, division, request, _data;
      var target = (e.currentTarget) ? e.currentTarget : e.srcElement;
      $query = $(target);
      _data = {};
      _data.q = $query.val();
      if (_data.q.length > 2) {
        return request = $.ajax(_url, {
          data: _data,
          success: searchCallback,
          error: searchFailure
        });
      } else {
        return $('#search-results').html("");
      }
    };

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

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