简体   繁体   English

Javascript跨浏览器脚本

[英]Javascript cross browser scripting

I have a tiny function I use to only allow numeric input. 我有一个很小的功能,我只允许数字输入。 It works great in IE, but I cannot get it to work in FireFox or Chrome. 它在IE中效果很好,但我无法在FireFox或Chrome中正常工作。 I have this js file loaded in my script tag of my HTML page. 我在我的HTML页面的脚本标签中加载了这个js文件。

var numberOnly = function(evt) { 

  var theEvent = evt || window.event; 
  var key = theEvent.keyCode || theEvent.which; 
  key = String.fromCharCode( key ); 
  var regex = /[0-9]|\./; 
  if( !regex.test(key) ) { 
    theEvent.returnValue = false; 

 } 
}; 

var wireElemToEvent = function(elemId, event, func){

var elem = document.getElementById(elemId);
if (typeof window.event !== 'undefined') {
    elem.attachEvent("on" + event, func);
} else {
    elem.addEventListener(event, func, true);
}
};



var wireEvents = function(){

wireElemToEvent("tbxQuantity", "keypress", numberOnly);
wireElemToEvent("tbxPhone", "keypress", numberOnly);
wireElemToEvent("tbxZip", "keypress", numberOnly);


};

window.onload = wireEvents;

Chrome tells me Chrome告诉我

file:///C:/Documents%20and%20Settings/xxx/Desktop/numbersonly/res/js/numbersonly.js:17Uncaught TypeError: Object #<an HTMLInputElement> has no method 'attachEvent' file:/// C:/Documents%20and%20Settings/xxx/Desktop/numbersonly/res/js/numbersonly.js:17未被捕获的TypeError:对象#<一个HTMLInputElement>没有方法'attachEvent'

Any idea what I am doing wrong? 知道我在做什么错吗?

In wireElemToEvent You may want to check that elem is not null after you initialize it. wireElemToEvent您可能需要在初始化elem后检查它是否不为null。 Also, it would be better to check the existence of elem.attachEvent and elem.addEventListener rather than whether window.event is defined. 同样,最好检查elem.attachEventelem.addEventListener的存在,而不是检查是否定义了window.event

Here is the function I use to attach events cross browser: 这是我用来跨浏览器附加事件的函数:

 function eventListen( t, fn, o ) {
  o = o || window;
  var e = t+fn;
  if ( o.attachEvent ) {
   o['e'+e] = fn;
   o[e] = function(){
    o['e'+e]( window.event );
   };
   o.attachEvent( 'on'+t, o[e] );
  }else{
   o.addEventListener( t, fn, false );
  }
 }

And to use it: 并使用它:

eventListen('message', function(e){
 var msg = JSON.parse(e.data);
    ...
});

I've had the same problem and, because I am a novice, was looking around for a day for a solution. 我遇到了同样的问题,并且由于我是新手,所以四处寻找解决方案。 Apparently (typeof window.event !== 'undefined') doesn't stop Safari/Chrome from getting in that if statement. 显然(typeof window.event!=='undefined')不会阻止Safari / Chrome进入该if语句。 So it actually initializes the attachEvent and doesn't know what to do with it. 因此,它实际上初始化了attachEvent,但不知道该如何处理。

Solution: 解:

var wireElemToEvent = function(elemId, event, func){

var elem = document.getElementById(elemId);
if (elem.attachEvent) {
    elem.attachEvent("on" + event, func);
} 
else { // if (elem.addEventListener) interchangeably
    elem.addEventListener(event, func, true);
}
};

One good way to make cross-browser scripting easier is to use jQuery . 简化跨浏览器脚本编写的一种好方法是使用jQuery Plus, there's no reason to reinvent the wheel. 另外,没有理由重新发明轮子。 Why not try this jQuery plugin: jQuery » numeric 为什么不尝试这个jQuery插件: jQuery»数字

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM