gpt4 book ai didi

Javascript 绑定(bind)函数事件数据为空或不是 IE 8 中的对象

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:35:32 25 4
gpt4 key购买 nike

这在除 IE 之外的任何地方都工作正常,当您单击 #doorbell IE 说 view.knock 为 null 或不是对象并失败时,我猜它与 enableKnock 中的绑定(bind)函数有关,但它不是传递事件数据?

已解决:我将 detectKnock 函数更改为:

detectKnock: function(e) {
e = e || window.event;
e.data.knockInput.push({
time: (new Date()).getTime()
});

clearTimeout(e.data.completeId);
e.data.completeId = setTimeout(e.data.completeKnock, 1200);
e.preventDefault();
return false;
},

错误发生在这里:

e.view.knock.knockInput.push({
time: (new Date()).getTime()
});

HTML

<button id="doorbell">Push Me</button>

Javascript

var knock = Object();
knock = {
successCallback: '',
secretKnock: '',
knockInput: [],
reject: 0.25,
averageReject: 0.15,
fadeTime: 150,
completeTime: 1200,
completeId: '',
lastNow: '',
clickEventType: ((document.ontouchstart!==null) ? 'click':'touchstart'),

enableKnock: function (callback, knock) {
this.successCallback = callback;
this.secretKnock = knock;
$('#doorbell').bind(this.clickEventType, this, this.detectKnock);
},
disableKnock: function() {
$('doorbell').unbind(this.clickEventType, this.knock.detectKnock);
clearInterval(completeId);
},
detectKnock: function(e) {
var now = new Date().getTime();
e.view.knock.knockInput.push({
time: (new Date()).getTime()
});

clearTimeout(e.view.knock.completeId);
e.view.knock.completeId = setTimeout(e.view.knock.completeKnock, 1200);
e.preventDefault();
return false;
},
completeKnock: function() {
if (this.knock.validateKnock()) {
this.knock.successCallback();
}
this.knock.knockInput = [];
},
validateKnock: function() {
var maxTime = 0;
var times = [];
var time;
var result = true;
var dist, i;
var timeDiff, totalDiff;

if (this.secretKnock.length == this.knockInput.length) {
for (i = 1; i < this.knockInput.length && result; ++i) {
time = this.knockInput[i].time - this.knockInput[i - 1].time;
times.push(time);
maxTime = Math.max(maxTime, time);
}

for (i = 0; i < times.length && result; ++i) {
timeDiff = Math.abs((times[i] / maxTime) - this.secretKnock[i].delay);
totalDiff += timeDiff;
if (timeDiff > this.reject) {
result = false;
}
}

if (result && totalDiff / times.length > this.averageReject) {
result = false;
}
} else {
result = false;
}
return result;
}
};

// Shave-and-a-haircut, two bits!
var secret = [
{delay:0.5},
{delay:0.25},
{delay:0.25},
{delay:0.5},
{delay:1},
{delay:0.5},
{delay:0}
];

knock.enableKnock(allowInside, secret);

function allowInside() {
$('#sub_header').remove();
$('#images').html('<div class="row" id="welcome"><div class="span12"><h1>Welcome to the afterparty.</h1></div></div>');
$('body').fadeOut(7500);
setTimeout( function() { window.location='party.html' }, 1500 );
}

最佳答案

在 IE 8 上,事件 DTO 只是窗口对象的成员,不会传递给事件监听器,因此对 e 的任何尝试调用都会返回 null:

detectKnock: function(e) {
var now = new Date().getTime();
e.view.knock.knockInput.push({
time: (new Date()).getTime()
});

clearTimeout(e.view.knock.completeId);
e.view.knock.completeId = setTimeout(e.view.knock.completeKnock, 1200);
//---^----- error on null access
e.preventDefault();
return false;
}

来自 quirksmode.org:

In the Microsoft event accessing model there is a special property window.event that contains the last event that took place.

Source

关于Javascript 绑定(bind)函数事件数据为空或不是 IE 8 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12290080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com