1
在Firefox中,代码的这部分似乎永远不会返回true。然而,它的工作原理非常清楚谷歌浏览器为什么这个函数= true在谷歌浏览器中工作,但不是Firefox?
if(restrictCharacters(this, event, digitsOnly)==true)
什么应该发生的是,如果用户在输入框中输入一个从1数到5的ID E1Rating该函数返回true,并运行代码嵌套下方。但在Firefox中没有任何事情发生后,参与者输入数字。
我将代码剥离到Firefox中不工作的部分。所以你可以看到它是如何使用的。
document.getElementById("E1TimeStart").value = startTime;
$('#E1Rating').keyup(function() {
if(restrictCharacters(this, event, digitsOnly)==true){
clearTimeout(mytimeout);
var rateTime = new Date().getTime();
$('#E1Rate').hide();
document.getElementById("E1TimeEnd").value = rateTime;
PrepareBox2();
}
else{
//Do nothing and wait for the timeout
}
});
};
这是restrictCharacters函数。我知道它可以工作,因为它再次在Chrome中运行。如果你在if == true函数之外使用它,该函数也可以在Firefox中使用。我对搜索和尝试的怀疑是它可能是(this,event,digitsOnly)中的事件引用。但是,如果是这种情况,具体是什么?
/* Purpose: Code will restrict false until it detects the numbers 1 through 5 */
/* Code Source: originally from qodo.co.uk */
// create as many regular expressions here as you need:
var digitsOnly = /[1-5]/g;
function restrictCharacters(myfield, e, restrictionType) {
if (!e) var e = window.event
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
var character = String.fromCharCode(code);
// if they pressed esc... remove focus from field...
if (code==27) { this.blur(); return false; }
// ignore if they are press other keys
// strange because code: 39 is the down key AND ' key...
// and DEL also equals .
if (!e.ctrlKey && code!=9 && code!=8 && code!=36 && code!=37 && code!=38 && (code!=39 || (code==39 && character=="'")) && code!=40) {
if (character.match(restrictionType)) {
return true;
} else {
return false;
}
}
}
谢谢!我将在哪里了解这些浏览器差异?我尝试搜索Chrome和Firefox JavaScript事件差异,但无法找到您告诉我的内容。 – OneBigNewbie 2012-02-09 23:08:00
对于Firefox和大多数其他浏览器,我在搜索关键字前加上“mdn”,例如“mdn event” - > [Mozilla Developer Network> DOM> Event](https://developer.mozilla.org/en/DOM/事件)。对于IE浏览器,我搜索了“msdn javascript event”,并得到了[这个页面](http://msdn.microsoft.com/en-us/library/ms535863%28v=vs.85%29.aspx)。 – 2012-02-10 15:00:39