In the work, writing JS often encountered some errors that do not meet the expectations, hereby recorded:
1. Firefox and Google about the KeyCode event compatibility: wrote a JS control input box can only enter numbers, but found in Google Browser can be used, but in Firefox is not allowed to enter the number.
function (event) { varnull); var keycode=E.keycode; if ((keycode >= && keycode <=) | | keycode = 8) { else } Returnfalse; } })
This code in Firefox, it is not possible to enter the number, and then found that each output keycode found to be 0, the feeling of getting keycode error, and then check the data found that Firefox KeyCode and Google's acquisition method is not the same. So the code is modified to:
$ (". Money"). Live ("KeyPress", function ( Event) { var e = event? Event:(window.event? Window.event: null ); var keycode=0; // Here are different keycode=e.keycode| | e.which| | e.charcode; if ((keycode >= && keycode <=) | | keycode = 8" {} else { return false ; } })
In this way, Google and Firefox can be compatible with the number of input restrictions. Of course, if the paste copy in the past can not be controlled, so after the loss should also do a check.
2. About JS Digital size comparison problem:
1 var first = ' 1234 '; 2 var = ' second '; 3 Console.log (first < second); True
This is because this is a string comparison, according to a letter-by-letter comparison, comparing the first letter 1, equal, the second letter 3>2, so true;
Usually use jquery to get the value of the input box is actually this form if the direct comparison is definitely wrong. Need to be replaced by a digital comparison
var first = ' 1234 '; var = ' second '/ / true/ / equals to var first = 1234; var second = PPI//true
JS errors encountered at work (supplement at any time)