问题
i have a Java Script Code that can change Keyboard Language when focus on text box. but this code just work in IE and cant work correctly in Firefox or Opera my code :
<script type="text/javascript" language="javascript">
var farsi = true ;
var s = new Array(32,33,34,35,36,37,1548,1711,41,40,215,43,
1608,45,46,47,48,49,50,51,52,53,54,55,56,
57,58,1603,44,61,46,1567,64,1616,1584,125,
1609,1615,1609,1604,1570,247,1600,1548,47,
8217,1583,215,1563,1614,1569,1613,1601,
8216,123,1611,1618,1573,126,1580,1688,
1670,94,95,1662,1588,1584,1586,1610,1579,
1576,1604,1575,1607,1578,1606,1605,1574,
1583,1582,1581,1590,1602,1587,1601,1593,
1585,1589,1591,1594,1592,60,124,62,1617);
//==============================================
function change()
{
var KeyID =event.keyCode;
if(KeyID >= 128)
{
alert("تغيير دهيد EN لطفا زبان صفحه کليد را به");
event.keyCode=0;
return false;
}
if(KeyID > 47 && KeyID < 58) return true;
if(KeyID < 32)return true;
if ( KeyID>32 && KeyID<128) event.keyCode = s[KeyID-32] ;
}
</script>
回答1:
Here:
> var KeyID =event.keyCode;
other browsers use event.which, so:
var KeyID = event.which || event.keyCode;
More generically you could do:
var keyProp = typeof event.which == 'number'? 'which' : 'keyCode';
then:
var keyCode = event[keyProp];
Edit
If you're still having issues, perhaps the W3C DOM Events specification will help. You may note that the keyCode, which and charCode properties of Event objects are readonly, though of course that section of the specification is informational, not normative, so browsers may or may not behave otherwise.
来源:https://stackoverflow.com/questions/11894132/auto-change-keyboard-language-using-javascript-not-work-without-ie