jquery 小问题

passself 2010-04-08 03:31:36

<!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=utf-8" />
<title>无标题文档</title>
<script src="jquery-1.3.2.js"></script>
<script>
$(document).ready(function(){
$("#test1").focus();
});
</script>
</head>

<body>
<form name="form1" id="test_form1">
<input type="text" name="test1" id="temp1" />
<input type="text" name="test2" id="temp2" />
</form>
<form name="form2" id="test_form2">
<input type="text" name="test3" id="temp3" />
<input type="text" name="test4" id="temp4" />
</form>
<select id="select">
<option>语言选择</option>
<option>CN</option>
<option>EN</option>
</select>
</body>
</html>


我想要的效果是:当这四个输入框输入文字的时候,我点击选择语言的下拉框之后,焦点还回到刚才正在输入文字的输入框中怎么办,谢谢大家,jquery或者js的代码都可以
...全文
90 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
shan1119 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 lieri111 的回复:]
以上代码在第二个第三个和第四个可以,在默认的第一个中是不行的
[/Quote]

<script>
var obj;
$(document).ready(function(){
obj=$("input:first");
$("input").focus(function(){
obj=this;
});
$("select").change(function(){
if(obj)obj.focus();
});
});
</script>
usersname 2010-04-08
  • 打赏
  • 举报
回复
次序要调整一下,先设置事件在活动焦点,这obj才有值

    $("input[type=text]").focus(function() {
obj = this;
});
$("#temp1").focus();
passself 2010-04-08
  • 打赏
  • 举报
回复
以上代码在第二个第三个和第四个可以,在默认的第一个中是不行的
usersname 2010-04-08
  • 打赏
  • 举报
回复
后面的效果挺难实现,没有想到什么好的方法判断下拉框是否关闭。
passself 2010-04-08
  • 打赏
  • 举报
回复
就没有焦点了
passself 2010-04-08
  • 打赏
  • 举报
回复
大家好,你们在ff下试了吗?在我这里为什么select改变之后就可以焦点了
shan1119 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 vnetcbd 的回复:]
2楼的 学习了

有个问题

如果 select change不改变值

怎么办??就返回不了上一次input的焦点了
[/Quote]
$("select").change(function(){
if(obj)obj.focus();
});
$("select").blur(function(){
if(obj)obj.focus();
});
teerhu 2010-04-08
  • 打赏
  • 举报
回复
<html>
<head>
<script src="jquery-1.3.2.min.js"></script>
<script>
$(function(){
var cur;
$('input[type=text]').blur(function(){ cur=$(this);});

$("#select").change(function(){ if(cur) cur.focus(); });

});


</script>
<body>
<form name="form1" id="test_form1">
<input type="text" name="test1" id="temp1" />
<input type="text" name="test2" id="temp2" />
</form>
<form name="form2" id="test_form2">
<input type="text" name="test3" id="temp3" />
<input type="text" name="test4" id="temp4" />
</form>
<select id="select">
<option>语言选择</option>
<option>CN</option>
<option>EN</option>
</select>
</body>
</html>
usersname 2010-04-08
  • 打赏
  • 举报
回复
obj变量可以闭包
<!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=utf-8" />
<title>无标题文档</title>
<!--script src="jquery-1.3.2.js"></script-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
var obj;
$("#temp1").focus();
$("input[type=text]").focus(function() {
obj = this;
});
$("#select").change(function() {
obj.focus();
});
});
</script>
</head>

<body>
<form name="form1" id="test_form1">
<input type="text" name="test1" id="temp1" />
<input type="text" name="test2" id="temp2" />
</form>
<form name="form2" id="test_form2">
<input type="text" name="test3" id="temp3" />
<input type="text" name="test4" id="temp4" />
</form>
<select id="select">
<option>语言选择</option>
<option>CN</option>
<option>EN</option>
</select>
</body>
vnetcbd 2010-04-08
  • 打赏
  • 举报
回复
2楼的 学习了

有个问题

如果 select change不改变值

怎么办??就返回不了上一次input的焦点了
usersname 2010-04-08
  • 打赏
  • 举报
回复
明明是temp为何要写成test
<!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=utf-8" />
<title>无标题文档</title>
<!--script src="jquery-1.3.2.js"></script-->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#temp1").focus();
$("#select").change(function() {
$("#temp1").focus();
});
});
</script>
</head>

<body>
<form name="form1" id="test_form1">
<input type="text" name="test1" id="temp1" />
<input type="text" name="test2" id="temp2" />
</form>
<form name="form2" id="test_form2">
<input type="text" name="test3" id="temp3" />
<input type="text" name="test4" id="temp4" />
</form>
<select id="select">
<option>语言选择</option>
<option>CN</option>
<option>EN</option>
</select>
</body>
shan1119 2010-04-08
  • 打赏
  • 举报
回复

<!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=utf-8" />
<title>无h・文档</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="jquery-1.3.2.js"></script>
<script>
var obj;
$(document).ready(function(){
$("input").focus(function(){
obj=this;
});
$("select").change(function(){
if(obj)obj.focus();
});
});
</script>
</head>

<body>
<form name="form1" id="test_form1">
<input type="text" name="test1" id="temp1" />
<input type="text" name="test2" id="temp2" />
</form>
<form name="form2" id="test_form2">
<input type="text" name="test3" id="temp3" />
<input type="text" name="test4" id="temp4" />
</form>
<select id="select">
<option>寀言 宣b</option>
<option>CN</option>
<option>EN</option>
</select>
</body>
</html>
vnetcbd 2010-04-08
  • 打赏
  • 举报
回复
晕,太多焦点了,不好 返回上一次输入框的焦点

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧