我需要找到其他方法如何使用javascript函数。
var b = ce("input"); // Here I create element.
b.setAttribute("name", "g4");
b.value = "Centimetrais(pvz:187.5)";
b.onfocus = function() { remv(this); };
b.onchange = function() { abs(this); };
b.onkeypress = function() { on(event); }; // I need to change this place becose then I pass "event" argument function doesn't work.
ac(1, "", b); // Here I appendChild element in form.
以下是功能:
function on(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;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
在IE和Chrome中它可以工作但在mozilla中却没有。任何替代方法如何为firefox修复它?
另外在这条路径上工作在mozilla的其他功能如果通过其他参数如“car”,“dog”,这个。例如:
firstFunction();
function firstFunction() {
var b = ce("input"); // Here I create element.
b.onkeypress = function() { on("hi!"); };
ac(1, "", b); // Here I appendChild element in form.
}
function on(evt) {
alert(evt);
}
答案 0 :(得分:0)
如果我正确理解你的问题,问题是你不接受主处理程序中的事件参数。例如,在你的第一个例子中:
b.onkeypress = function() { on(event); };
应该是
b.onkeypress = function(e) { on(e || window.event); };
// Changes here --------^ and --^^^^^^^^^^^^
你在on
中这样做,但在on
中,已经太晚了,你已经失去了提供给onXYZ
函数的论据。
您的代码在Chrome和IE中运行的原因是IE使用全局event
对象而不是(或在现代版本中)实际传递到处理程序中的对象,Chrome会复制该行为与最期望的网站兼容。 Firefox没有。