最近在项目中碰到这样一个问题,表单提交,是用javascript控制的,当用户回车时就代表提交,同事在网上搜了一段代码放进去,在IE下是正常的,但我在用FF查看时,发现提交无效,用Firebug跟踪得到的结果是: window.event has no properties
程序代码
function document.onkeydown(){
var source=event.srcElement.id;
var btnid;
if (event.keyCode == 13 ){
if (source=="txtEmpCode"){
btnid="btnQry"
var btn = document.getElementById(btnid);
if (btn != event.srcElement){
btn.click();
return false;}
}else {
return false;
}
}
}
从获取事件的源对象及ID属性,然后到提交按钮的click事件,看上去是没有问题(项目中用的全是struts标签),但确忽略了FireFox中event与IE中的异同,IE下,event对象有srcElement属性,但是没有target属性;Firefox下,event对象有target属性,但是没有srcElement属性.但他们的作用是相当的;网上有资料说可以写成: var eventSource =window.event.srcElement||eventTag.target; 就解决了,但实际上是不行的,FF下既然不能识别window.event,又哪来的window.event.srcElement呢?所以会出现错误: window.event has no properties;
程序代码
function enter_another(eventTag)
{
var events = eventTag||window.event;
var eventSource;
var currentKey = events.charCode||events.keyCode;
if(document.all)
eventSource =window.event.srcElement;
else
eventSource = events.target;
if( currentKey == 13 )
alert("您按了回车键,输入的内容为"+eventSource.value);
}
或直接写成
程序代码
function enter_another(events)
{
var eventSource;
var currentKey = events.charCode||events.keyCode;
if(document.all)
eventSource =window.event.srcElement;
else
eventSource = events.target;
if( currentKey == 13 )
alert("您按了回车键,输入的内容为"+eventSource.value);
}
html代码引用:
程序代码
<input type="text" name="pwd" οnkeydοwn="enter_another(event)" />
修正代码:
程序代码
function enter_down(events){
var eventSource;
var currentKey=events.charCode||events.keyCode;
if(document.all)
eventSource =window.event.srcElement;
else
eventSource = events.target;
var source=eventSource.id;
var btnid;
if (currentKey == 13 ){
if (source=="txtEmpCode"){
btnid="btnQry";
var btn = document.getElementById(btnid);
if (btn != eventSource){
document.getElementById("logform").submit();
return false;}
}else {
return false;
}
}
}
如果var currentKey=events.charCode||events.keyCode;按浏览器分开写的话,会导致得到的currentKey值永远为0;
程序代码
<html:password property="user.passwd" size="15" styleId="txtEmpCode" οnkeydοwn="enter_down(event)" styleClass="login_input" tabindex="2"/>
标签:FireFox,events,srcElement,window,eventSource,var,IE,event From: https://blog.51cto.com/u_15834343/5887914