在做一个网页版的打字聊天项目时,要时时检测按键的键值,用onkeydown事件获得的键值总是229。经过研究找出了原因,换成onkeyup事件就可以了。另外还有一个问题:用外挂输入法(如小小输入法)在输入汉字时,每个字总是多出一键,经过研究找出,原来总是自动加上了一个231的键值,我的解决方法是把返回值为231的跳过就行了。
我的代码片段是: <textarea class="box" id="typeInput" onkeyup="checkKey()" onpaste="return false" onPropertyChange="checkduicuo()"></textarea> //检测按下的键 function checkKey(){ if (event.keyCode != 231) jijiancishu++; //击键次数 zongjianshu.innerText=jijiancishu; //总击键次数 if (event.keyCode == 8){tuigecishu++;} //退格数 tuige.innerText=tuigecishu; if (event.keyCode == 13){event.keyCode=0;return false;}//屏蔽回车键 }
//时时判断对错 function checkduicuo() { text1=typeContent.innerText; text2=$("typeInput").value; ...... ...... } |