<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>文档</title>
<script language="JavaScript" type="text/javascript">
function test(obj){
alert(obj.value)
obj.focus();
}
</script>
</head>
<body >
<table id="table1" width="200" border="1">
<tr>
<td><input type=text id="txt1" onchange="test(this)" /></td>
<td>aa</td>
</tr>
<tr>
<td>b</td>
<td><select id="selid"><option value=1>sel1</option><option value=2>sel2</option></select></td>
</tr>
</table>
</body>
</html>
为什么我在input框输入文字后,鼠标直接点击select标签的话,
触发完JS函数后,焦点是在select标签上的,
而不是在input上
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>文档</title>
<script language="JavaScript" type="text/javascript">
function test(obj){
alert(obj.value)
obj.focus();
}
</script>
</head>
<body >
<table id="table1" width="200" border="1">
<tr>
<td><input type=text id="txt1" onchange="test(this)" /></td>
<td>aa</td>
</tr>
<tr>
<td>b</td>
<td><select id="selid"><option value=1>sel1</option><option value=2>sel2</option></select></td>
</tr>
</table>
</body>
</html>
为什么我在input框输入文字后,鼠标直接点击select标签的话,
触发完JS函数后,焦点是在select标签上的,
而不是在input上
不是说alert 能把程序停下来吗?
<input type=text id="txt1" onchange="test(this)" />
这个只有在input里面的值被修改了后才触发事件的
你可以换成blur
<input type=text id="txt1" onblur="test(this)" />
js那时候还没结束,是点了确定后才运行focus的,照你这么说,为什么我点其他的控件,比如textbox是正确的
to 3楼:
所有触发类型都一样 都会定位到select的
onblur也一样,只要鼠标移动到select上就这样
不信 你就建个txt文本,复制一下楼主的代码 自己用onblur测试一下
<input type=text id="txt1" onblur="test(this)" />楼主在input中一般用onblur这个事件,当是select的时候用onchange
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>文档</title>
<script language="JavaScript" type="text/javascript">
function test(obj){
alert(obj.value)
//obj.focus();
window.setTimeout("document.getElementById('txt1').focus();", 50) ;
}
</script>
</head>
<body >
<table id="table1" width="200" border="1">
<tr>
<td><input type=text id="txt1" onchange="test(this)" /></td>
<td>aa</td>
</tr>
<tr>
<td>b</td>
<td><select id="selid"><option value=1>sel1</option><option value=2>sel2</option></select></td>
</tr>
</table>
</body>
</html>
参考资料
3个浏览器都是一样的,即在select获取焦点后,焦点马上闪回'txt1'
点击其他textbox(即input),或者点击空白处是可以把通过obj.focus回去的,只是点击了dropdownlist(即select)焦点回不去
焦点当然会在select上,不在的话才会奇怪呢
不是这个的,我测试过了如果你点到input text也是不行的他也会把焦点放在你点击的这个
text上