为什么这个函数在谷歌浏览器中是真的,而在火狐浏览器中则不然
Why is this function = true working in Google Chrome but not Firefox?
在Firefox中,这部分代码似乎从未返回true。然而,它在谷歌Chrome 中运行得非常好
if(restrictCharacters(this, event, digitsOnly)==true)
应该发生的是,如果用户在ID为E1Rating的输入框中输入一个从1到5的数字,则函数将返回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,digitalsOnly)中的事件引用。但如果是这样的话,具体是什么?
/* 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;
}
}
}
您必须将event
定义为函数参数。Google Chrome和IE支持非标准的全局event
属性,该属性指的是最新的事件对象。
$('#E1Rating').keyup(function(event) { //<--
if(restrictCharacters(this, event, digitsOnly)){
注意:if (.. == true)
可以安全地替换为if (...)
,因为任何等于true的表达式作为断言也是true。
相关文章:
- 如何在火狐浏览器中禁用F5和刷新
- 在火狐浏览器卸载之前
- 如何在火狐浏览器上增加 IndexedDB 的配额
- 多个 socket.io 客户端连接不起作用(谷歌浏览器,火狐浏览器)
- 火狐浏览器不会在前进时触发流行状态事件
- 在火狐浏览器的画布上获取鼠标位置
- 调用本机浏览器函数,即使它已被覆盖
- d3.火狐浏览器中的鼠标像素偏移量
- 使用量角器打开火狐浏览器的文件网址
- 火狐浏览器 iframe 设计模式不起作用
- $injector:nomod模块“应用程序”不可用!仅限火狐浏览器
- 以编程方式打开火狐浏览器并执行 POST 请求?[想法:MozRepl,建议?
- 为什么我在火狐浏览器中剪辑的图像中有边框而不是铬
- 火狐浏览器与 event.target.id 的问题
- 帖子数据在火狐浏览器上不起作用
- 动态.js在火狐浏览器上很慢
- 关闭火狐浏览器的平滑滚动
- 火狐浏览器上的函数表达式 - 不是预期的结果
- JavaScript 显示隐藏函数布局问题火狐浏览器
- 为什么这个函数在谷歌浏览器中是真的,而在火狐浏览器中则不然