Javascript绑定函数事件数据为null或不是IE 8中的对象
除了IE,这在任何地方都可以正常工作,当你点击#doorbell IE说view.knock为null或者不是一个对象并且失败时,我猜这与enableKnock中的bind函数及其未传递事件数据有关 已解决:我将detectKnock功能更改为:Javascript绑定函数事件数据为null或不是IE 8中的对象,javascript,jquery,internet-explorer-8,Javascript,Jquery,Internet Explorer 8,除了IE,这在任何地方都可以正常工作,当你点击#doorbell IE说view.knock为null或者不是一个对象并且失败时,我猜这与enableKnock中的bind函数及其未传递事件数据有关 已解决:我将detectKnock功能更改为: detectKnock: function(e) { e = e || window.event; e.data.knockInput.push({ time: (new Date()).getTime() })
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
推我
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 );
}
var knock=Object();
爆震={
成功回调:“”,
秘笈,
输入:[],
拒绝:0.25,
平均喷射:0.15,
fadeTime:150,
完成时间:1200,
completeId:“”,
lastNow:“,
clickEventType:((document.ontouchstart!==null)?“单击”:“touchstart”),
enableKnock:函数(回调、敲打){
this.successCallback=回调;
this.secretKnock=敲打;
$('#doorbell').bind(this.clickEventType,this,this.detectKnock);
},
disableKnock:function(){
$('doorbell').unbind(this.clickEventType,this.knock.detectKnock);
clearInterval(completeId);
},
检测爆震:功能(e){
var now=new Date().getTime();
e、 view.knock.knockInput.push({
时间:(新建日期()).getTime()
});
clearTimeout(e.view.knock.completeId);
e、 view.knock.completeId=设置超时(e.view.knock.completeKnock,1200);
e、 预防默认值();
返回false;
},
completeKnock:function(){
if(this.knock.validateKnock()){
this.knock.successCallback();
}
this.knock.knockInput=[];
},
validateKnock:function(){
var maxTime=0;
var时间=[];
var时间;
var结果=真;
var-dist,i;
var-timeDiff,totalDiff;
if(this.secretKnock.length==this.knockInput.length){
对于(i=1;ithis.reject){
结果=假;
}
}
if(result&&totalDiff/times.length>this.averageReject){
结果=假;
}
}否则{
结果=假;
}
返回结果;
}
};
//剃胡子剪头发,两刀!
变量秘密=[
{延迟:0.5},
{延迟:0.25},
{延迟:0.25},
{延迟:0.5},
{延迟:1},
{延迟:0.5},
{延迟:0}
];
knock.enableKnock(allowInside,secret);
函数allowInside(){
$(“#子#标题”).remove();
$(“#images”).html(‘欢迎参加晚会’);
$('body')。淡出(7500);
setTimeout(函数(){window.location='party.html'},1500);
}
在IE 8上,事件DTO只是窗口对象的成员,没有传递给事件侦听器,因此在e
上的任何尝试调用都返回null
:
检测爆震:功能(e){
var now=new Date().getTime();
e、 view.knock.knockInput.push({
时间:(新建日期()).getTime()
});
clearTimeout(e.view.knock.completeId);
e、 view.knock.completeId=设置超时(e.view.knock.completeKnock,1200);
//---^-----空访问错误
e、 预防默认值();
返回false;
}
来自quirksmode.org:
在Microsoft事件访问模型中,有一个特殊属性
包含上次发生的事件的window.event
我在Chrome中运行JSFIDLE时也遇到了同样的错误<代码>无法读取未定义的属性“knockInput”。这似乎不仅仅是IE中的情况。当运行时:
console.log(e.view)
在Chrome中它是window
object。有趣的是,你能提供一个现场演示来重现它吗?在Chrome中试试这个。当我单击按钮时,控制台输出:uncaughttypeerror:当我添加console.log(e.view)时,无法读取未定义的属性“knockInput”
作为detectKnock
方法中的第一行,它在error.ooops之前输出窗口对象>window
,这就是jQuery。忘了它吧,事件不是窗口的成员。请取消接受我的答案,以便我可以删除它。我们不想欺骗任何人。
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 );
}