说不清楚具体的原因,
从现象上看,浏览在处理焦点的时候需要一点时间。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<table border="0" width="628" cellspacing="0" cellpadding="0">
<tr>
<td width="27">
</td>
<td width="109">
<input onclick="this.select()" id="时" class="inp4" maxlength="12" value=12>
</td>
<td width="49">时</td>
<td width="139">
<input onclick="this.select()" id="分" class="inp4" maxlength="8" value=43>
</td>
<td width="37">分</td>
<td width="140">
<input onclick="this.select()" id="秒" class="inp4" maxlength="8" value=34>
</td>
<td width="51">秒</td>
</tr>
</table>
<hr>
<div id="div1">
</div>
</body>
<script language="JavaScript">
shi = document.all["时"];
fen = document.all["分"];
mio = document.all["秒"];
var nn = 1
document.onkeypress = function(){
div1.innerHTML += "{keypress}";
if (window.event.keyCode == 13) {
if (nn == 1) {
shi.select();
}
if (nn == 2) {
fen.select();
}
if (nn == 3) {
mio.select();
}
if (nn < 3) {
nn++
}
else {
nn = 1
}
}
}
var token = 0;
document.onmousedown = function(e){
if (window.event.button == 1 && !token) { //=1:单击了左键;=3:单击了右键
div1.innerHTML += "{mousedown}nn=" + nn;
if (nn == 1) {
window.setTimeout("token=1;shi.select();token=0;", 2);
}
if (nn == 2) {
window.setTimeout("token=1;fen.select();token=0", 2);
}
if (nn == 3) {
window.setTimeout("token=1;mio.select();token=0;", 2);
}
if (nn < 3) {
nn++
}
else {
nn = 1
}
}
}
</script>
</html>
从现象上看,浏览在处理焦点的时候需要一点时间。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<body>
<table border="0" width="628" cellspacing="0" cellpadding="0">
<tr>
<td width="27">
</td>
<td width="109">
<input onclick="this.select()" id="时" class="inp4" maxlength="12" value=12>
</td>
<td width="49">时</td>
<td width="139">
<input onclick="this.select()" id="分" class="inp4" maxlength="8" value=43>
</td>
<td width="37">分</td>
<td width="140">
<input onclick="this.select()" id="秒" class="inp4" maxlength="8" value=34>
</td>
<td width="51">秒</td>
</tr>
</table>
<hr>
<div id="div1">
</div>
</body>
<script language="JavaScript">
shi = document.all["时"];
fen = document.all["分"];
mio = document.all["秒"];
var nn = 1
document.onkeypress = function(){
div1.innerHTML += "{keypress}";
if (window.event.keyCode == 13) {
if (nn == 1) {
shi.select();
}
if (nn == 2) {
fen.select();
}
if (nn == 3) {
mio.select();
}
if (nn < 3) {
nn++
}
else {
nn = 1
}
}
}
var token = 0;
document.onmousedown = function(e){
if (window.event.button == 1 && !token) { //=1:单击了左键;=3:单击了右键
div1.innerHTML += "{mousedown}nn=" + nn;
if (nn == 1) {
window.setTimeout("token=1;shi.select();token=0;", 2);
}
if (nn == 2) {
window.setTimeout("token=1;fen.select();token=0", 2);
}
if (nn == 3) {
window.setTimeout("token=1;mio.select();token=0;", 2);
}
if (nn < 3) {
nn++
}
else {
nn = 1
}
}
}
</script>
</html>
解决方案 »
- 菜鸟向各位JS大神求救下
- 请问一下各位前辈,关于js调试,你们都用什么工具?
- Ext的tree显示的问题(解决立刻给分)
- <anchor>的问题
- 如何判断表单项里的用户名是字符和数字的组合
- 动态获取文本框的值。我用onkeydown,onkeyup总是慢半拍,输入123只能得到12
- 在窗口中打开了一模态对话框,可不可以在模态对话框中调用父窗口页面更新或关闭退出时让父窗口页面更新?
- 在javacript里怎么实现变量的名字有字符与变量的组合。
- 将topFrame的值赋给rightFrame.iframe问题。
- 如何让下拉框中的选项在打开页面时没有一个被选中!!!!!!!!!!!!!!!!!!!!!!!!!!
- 在js里写动态sql的问题
- 求一个js代码 能够选择版本号
<tr>
<td width="27"> </td>
<td width="109"> <input onclick="this.select()" id="时" class="inp4" maxlength="12" value=12> </td>
<td width="49">时 </td>
<td width="139"> <input onclick="this.select()" id="分" class="inp4" maxlength="8" value=43> </td>
<td width="37">分 </td>
<td width="140"> <input onclick="this.select()" id="秒" class="inp4" maxlength="8" value=34> </td>
<td width="51">秒 </td>
</tr>
</table> <script>
shi=document.all["时"];
fen=document.all["分"];
mio=document.all["秒"];
var nn=1 function document.onkeydown(){ //仅用于接收方向键
objid=document.activeElement.id;
if (event.keyCode==38){ //up 向上方向键
if(objid=="秒"){fen.select()}
if(objid=="分"){shi.select()}
if(objid=="时"){mio.select()}
}
if (event.keyCode==40){ //down 向下方向键
if(objid=="时"){fen.select()}
if(objid=="分"){mio.select()}
if(objid=="秒"){shi.select()}
}
}
function document.onmousedown(){
if(event.button==1){ //=1:单击了左键;=3:单击了右键
if(nn==1){window.setTimeout("shi.select();", 5);}
if(nn==2){window.setTimeout("fen.select();", 5);}
if(nn==3){window.setTimeout("mio.select();", 5);}
if(nn <3){nn++}else{nn=1}
//alert("单击了左键")
}
}
</script>
主要原因是原来代码中,下面这几条语句是在画面装载之前取得的对象.
因此取不到.shi = document.all["时"];
fen = document.all["分"];
mio = document.all["秒"];
修改后的代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<script language="JavaScript">
window.onload = function(){
shi = document.all["时"];
fen = document.all["分"];
mio = document.all["秒"];
}
var nn = 1;
//仅用于接收方向键
document.onkeydown = function(e){
e = e || window.event;
objid = e.srcElement.id;
if (event.keyCode == 38) { //up 向上方向键
if (objid == "秒") {
fen.select()
}
if (objid == "分") {
shi.select()
}
if (objid == "时") {
mio.select()
}
}
if (event.keyCode == 40) { //down 向下方向键
if (objid == "时") {
fen.select();
}
if (objid == "分") {
mio.select()
}
if (objid == "秒") {
shi.select()
}
}
}
document.onmousedown = function(){
if (event.button == 1) { //=1:单击了左键;=3:单击了右键
if (nn == 1) {
window.setTimeout("shi.select();", 5);
}
if (nn == 2) {
window.setTimeout("fen.select();", 5);
}
if (nn == 3) {
window.setTimeout("mio.select();", 5);
}
if (nn < 3) {
nn++
}
else {
nn = 1
}
//alert("单击了左键")
}
}
</script>
<body>
<table border="0" width="628" cellspacing="0" cellpadding="0">
<tr>
<td width="27">
</td>
<td width="109">
<input onclick="this.select()" id="时" class="inp4" maxlength="12" value=12>
</td>
<td width="49">
时
</td>
<td width="139">
<input onclick="this.select()" id="分" class="inp4" maxlength="8" value=43>
</td>
<td width="37">
分
</td>
<td width="140">
<input onclick="this.select()" id="秒" class="inp4" maxlength="8" value=34>
</td>
<td width="51">
秒
</td>
</tr>
</table>
</body>
</html>
还有,没留神你加的那句话,让我晕了好阵子。
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <script language="JavaScript">
document.onkeydown = function(e){
e = e || window.event;
objid = e.srcElement.id;
if (event.keyCode == 38) { //up 向上方向键
if (objid == "秒") {document.all["分"].select()}
if (objid == "分") {document.all["时"].select()}
if (objid == "时") {document.all["秒"].select()}
}
if (event.keyCode == 40) { //down 向下方向键
if (objid == "时") {document.all["分"].select();}
if (objid == "分") {document.all["秒"].select()}
if (objid == "秒") {document.all["时"].select()}
}
}
</script>
shi = document.all["时"];
fen = document.all["分"];
mio = document.all["秒"];
}这个你改过了吗?
你没说要选中呀.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
</head>
<script language="JavaScript">
window.onload = function(){
shi = document.all["时"];
fen = document.all["分"];
mio = document.all["秒"];
}
var nn = 1;
//仅用于接收方向键
document.onkeydown = function(e){
e = e || window.event;
objid = e.srcElement.id;
if (e.keyCode == 38) {
//up 向上方向键
if (objid == "秒") {
fen.select()
}
if (objid == "分") {
shi.select()
}
if (objid == "时") {
mio.select()
}
e.returnValue = false;
}
if (e.keyCode == 40) {
//down 向下方向键
if (objid == "时") {
fen.select();
}
if (objid == "分") {
mio.select()
}
if (objid == "秒") {
shi.select()
}
e.returnValue = false;
}
}
document.onmousedown = function(){
if (event.button == 1) {
//=1:单击了左键;=3:单击了右键
if (nn == 1) {
window.setTimeout("shi.select();", 2);
}
if (nn == 2) {
window.setTimeout("fen.select();", 2);
}
if (nn == 3) {
window.setTimeout("mio.select();", 2);
}
if (nn < 3) {
nn++
}
else {
nn = 1
}
//alert("单击了左键")
}
}
</script>
<body>
<table border="0" width="628" cellspacing="0" cellpadding="0">
<tr>
<td width="27">
</td>
<td width="109">
<input onclick="this.select()" id="时" class="inp4" maxlength="12" value=12>
</td>
<td width="49">
时
</td>
<td width="139">
<input onclick="this.select()" id="分" class="inp4" maxlength="8" value=43>
</td>
<td width="37">
分
</td>
<td width="140">
<input onclick="this.select()" id="秒" class="inp4" maxlength="8" value=34>
</td>
<td width="51">
秒
</td>
</tr>
</table>
</body>
</html>
难道,你在IE下不好用?
怎么可能,我在IE6和IE7下都测试过了.
并且刚刚我又测试了一下.
document.onkeydown = function(e){
e = e || window.event;
objid = e.srcElement.id;
if (event.keyCode == 38) { //up 向上方向键
if (objid == "秒") {document.all["分"].select()}
if (objid == "分") {document.all["时"].select()}
if (objid == "时") {document.all["秒"].select()}
}
if (event.keyCode == 40) { //down 向下方向键
if (objid == "时") {document.all["分"].select();}
if (objid == "分") {document.all["秒"].select()}
if (objid == "秒") {document.all["时"].select()}
}
}
</script>
你也没看我的代码呀!!!!!
没看到我的代码中加了 e.returnValue = false; 吗?
因为在浏览器默认方向键是用来移动光标的.
如果你不阻止浏览器的默认动作的话,
选择的状态就被浏览器移动光标的处理给取消了.
还有这一句没有用过,不好理解。
e = e || window.event;
最近对其进行改版后,便失去了效果。
不得以将原网页代码进行轮番精简,结果发现是缺了这个<form>,仅仅因为用不着他。
却耗了我不少时间,加上他效果就出来了。<form>
<table border="0" width="628" cellspacing="0" cellpadding="0">
<tr>
<td width="27"> </td>
<td width="109"> <input onclick="this.select()" id="时" class="inp4" maxlength="12" value=12> </td>
<td width="49">时 </td>
<td width="139"> <input onclick="this.select()" id="分" class="inp4" maxlength="8" value=43> </td>
<td width="37">分 </td>
<td width="140"> <input onclick="this.select()" id="秒" class="inp4" maxlength="8" value=34> </td>
<td width="51">秒 </td>
</tr>
</table> <script>
shi=document.all["时"];
fen=document.all["分"];
mio=document.all["秒"];
function document.onkeydown(e){ //仅用于接收方向键
e = e || window.event;
objid = e.srcElement.id;
if (event.keyCode==38){ //up 向上方向键
if(objid=="秒"){fen.select()}
if(objid=="分"){shi.select()}
if(objid=="时"){mio.select()}
}
if (event.keyCode==40){ //down 向下方向键
if(objid=="时"){fen.select()}
if(objid=="分"){mio.select()}
if(objid=="秒"){shi.select()}
}
// e.returnValue = false
}
</script>
这一句是为了兼容IE和firefox的。
IE是通过window.event取得当前事件,
firefox则是通过参数传递给函数的。