- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 Javascript 与 IE8 不兼容,我不确定如何解决它。我收到一个错误“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);
有谁知道我如何重新编码这段代码以使其在 IE 中正常运行?
谢谢!
最佳答案
我发现了问题。问题是这一行:
setTimeout(search, 300, e);
最简单的修复方法是:
setTimeout((function(e) { return function() { search(e) }; })(e), 300);
我将它包裹在一个闭包中以避免范围界定问题。
来自 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).
我只是从文档中复制所有内容,作为引用。
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.
/*\
|*|
|*| 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
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:
/*@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:
<!--[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));
关于javascript - IE8 event.currentTarget 为空或不是对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22692427/
当检测鼠标x和y坐标时,最好像这样使用event.clientX和event.clientY: function show_coords(event){ var x=event.clientX;
我有以下代码: document.oncontextmenu = function(evt) { evt = evt || window.event; console.log(evt.
对于另一个问题,我遇到了一个似乎偶尔出现在 SO 的误解。一些提问者似乎认为触发器之于数据库就像事件之于 OOP 一样。 有没有人有一个很好的类比来解释为什么这是一个有缺陷的比较,以及误用它的后果?
$('body').keypress(function(event){ if(event.keyCode == 46){console.log('Delete Key Pressed')};
我正在制作一个“流体”文本区域,它根据内容调整其高度。我实际上正在尝试实现 this脚本。我有以下代码:https://ellie-app.com/Vjtvm6yrKWa1/4 问题是,当增加高度时,
我使用 Raphael .mouseover() 和 .mouseout() 事件来突出显示 SVG 中的某些元素。这工作正常,但在我单击一个元素后,我希望它停止突出显示。 在 Raphael doc
我目前正在开发一个应用程序,允许人们为在线广播电台安排“节目”。 我希望用户能够设置重复事件,例如:- “躁狂星期一”节目 - 每周一 9 点至 11 点“月中疯狂” - 每个月的第二个星期四“本月新
我有以下三个表格(简化版本): 已加载关卡: id(整数、主键、自动增量) globalId(整数,键) 日期(日期时间、键) serverId(Int,键) gamemodeId(Int,Key)
在我阅读 Gevent Tutorial 之后,我有一个关于 gevent.event.Event 的问题。 Event.set() 是否会唤醒所有被 Event.wait() 阻塞的函数? 就像下面
我对 cakephp ver3.1.3 没有经验 我按照说明实现了登录认证功能; http://book.cakephp.org/3.0/en/tutorials-and-examples/blog-
现在,我发送 10 个事件,每个事件有 1 个属性。但是当我想过滤特定事件并按属性选择事件时,在“事件属性”过滤器中仅显示前 7 个事件,而我为其余事件添加的事件仅显示“第一次”过滤器,为什么? 最佳
我不知道我的 Firefox 发生了什么! 我的aspx和javascript代码是这样的: function a() { alert(
中有3个事件fns重装 ,我可以对两者做同样的事情 reg-event-db和 reg-event-fx . reg-event-db之间的主要区别是什么, reg-event-fx和 reg-eve
我遇到了 Firefox keydown 行为,因为在没有聚焦于特定字段的情况下按下 Enter 键(实际上是任何键)不会触发 keydown 事件只会触发`按键事件。 这可能会非常令人困惑,因为 k
这是我的代码片段 public class Notation : INotifyPropertyChanged { public event PropertyChangedEventHandl
我可以在一个 Jsf2 xhtml 文件中有多个标签吗? 在那种情况下,关联的监听器将以什么顺序被调用? Mojarra 2.1.1/Apache Tomcat 7.0.22/PrimeFaces 3
我可以在一个 Jsf2 xhtml 文件中有多个标签吗? 在那种情况下,关联的监听器将以什么顺序被调用? Mojarra 2.1.1/Apache Tomcat 7.0.22/PrimeFaces 3
我有以下 JavaScript: $('#ge-display').click(function (event) { window.open('/googleearth/ge-display.ph
我需要确定触发事件的元素。 使用 event.target 获取相应的元素。 我可以从那里使用哪些属性? 引用 编号 节点名 我找不到关于它的大量信息,即使在 jQuery 上也是如此页,所以希望有人
我在pyGame中创建了一个Asteroidz克隆,并在pygame.vent.get()循环中有两个for Event,一个用于检查退出请求,以及游戏是否应该通过按空格键开始,然后在游戏中进一步尝试
我是一名优秀的程序员,十分优秀!