三个输入框:
姓名<input type='text' name='txt1'><br />
手机<input type='text' name='txt2'><br />
昵称<input type='text' name='txt3'><br />
<button id='btn1'>下一个</button>
按钮描述:没有input获得焦点时,点击按钮第一个input获得焦点,有input获得焦点时,点击按钮下一个input获得焦点。
这个要怎么弄呢?当有INPUT获得焦点 当前INPUT的下一个元素呢?当按钮onclick被触发时,此时的focus是#documnet了,而不是input了?是不是?
要怎么做呢?
姓名<input type='text' name='txt1'><br />
手机<input type='text' name='txt2'><br />
昵称<input type='text' name='txt3'><br />
<button id='btn1'>下一个</button>
按钮描述:没有input获得焦点时,点击按钮第一个input获得焦点,有input获得焦点时,点击按钮下一个input获得焦点。
这个要怎么弄呢?当有INPUT获得焦点 当前INPUT的下一个元素呢?当按钮onclick被触发时,此时的focus是#documnet了,而不是input了?是不是?
要怎么做呢?
解决方案 »
- 使用javascript 放大、缩小图片
- AJAX提交遇到一个问题,高手请进~
- with(Div.style)中的Div.style是干什么意思?
- 关于window.open()函数的参数问题。
- 我想把在textarea中输入的文字的长度控制在500字以内,请问怎么写代码呢?
- 诚心请教:如何禁止页面或IE窗口切换
- Operation aborted错误
- [求助] 有关highcharts中类似 jfreechart 的createBufferedImage 方法
- 刚学java,有问题请教!tomcat和java
- js中如何打开文件夹
- 滚动条拖动时间改变效果怎么实现
- 急:论坛会员网绑定新浪微博账号
<!DOCTYPE HTML>
<html>
<head>
<meta charset="gb2312" />
<title></title>
<style>
</style>
</head>
<body>
姓名<input type='text' name='txt1'><br />
手机<input type='text' name='txt2'><br />
昵称<input type='text' name='txt3'><br />
<button id='btn1'>下一个</button>
<script>
var $ = function(id){
return document.getElementById(id);
};
var $t = function(tag, cot){
cot = cot || document;
return cot.getElementsByTagName(tag);
};
var objs = $t('input');
var i = 0;
$('btn1').onclick = function(){
objs[i].focus();
i = i > 1 ? 0 : i + 1;
}
</script>
</body>
</html>楼主 这个意思?
<head>
<script>
function xyz() {
var inputs1 = document.getElementsByTagName("input");
for (var i = 0; i < inputs1.length; i ++) {
if (inputs1[i].inputing == "true") {
inputs1[i].inputing = "false";
inputs1[(i + 1) % inputs1.length].focus();
return;
}
}
if (inputs1[0]) {
inputs1[0].inputing = "false";
inputs1[0].focus();
}
}
function initInputing(inputObject) {
var inputs2 = document.getElementsByTagName("input");
for (var i = 0; i < inputs2.length; i ++) {
if (inputs2[i] == inputObject) {
inputs2[i].inputing = "true";
}
else {
inputs2[i].inputing = "false";
}
}
}
</script>
</head>
<body>
<center>
姓名<input type="text" name="txt1" inputing = "false" onfocus = "initInputing(this)"><br/>
手机<input type="text" name="txt2" inputing = "false" onfocus = "initInputing(this)"><br/>
昵称<input type="text" name="txt3" inputing = "false" onfocus = "initInputing(this)"><br/>
<button id="btn1" onclick = "xyz()">下一个</button>
</center>
</body>
</html>
<html>
<head>
<script>
function xyz() {
var inputs1 = document.getElementsByTagName("input");
for (var i = 0; i < inputs1.length; i ++) {
if (inputs1[i].inputing == "true") {
inputs1[i].inputing = "false";
inputs1[(i + 1) % inputs1.length].focus();
return;
}
}
if (inputs1[0]) {
inputs1[0].inputing = "false";
inputs1[0].focus();
}
}
function initInputing(inputObject) {
var inputs2 = document.getElementsByTagName("input");
for (var i = 0; i < inputs2.length; i ++) {
if (inputs2[i] == inputObject) {
inputs2[i].inputing = "true";
}
else {
inputs2[i].inputing = "false";
}
}
}
</script>
</head>
<body>
<center>
姓名<input type="text" name="txt1" inputing = "false" onfocus = "initInputing(this)"><br/>
手机<input type="text" name="txt2" inputing = "false" onfocus = "initInputing(this)"><br/>
昵称<input type="text" name="txt3" inputing = "false" onfocus = "initInputing(this)"><br/>
<button id="btn1" onclick = "xyz()">下一个</button>
</center>
</body>
</html>
给每个INPUT加个属性~~通过这个做标识来切换,想过这样做,没有动手,如果不加自定义属性之类的,有什么方法没?
2楼的方法能实现 按键从第一个开始循环切换焦点,但是如果一开始已经有一个焦点存在,比如input[1]了,再按键切换就不能跳到下一个input了。
cot = cot || document;
return cot.getElementsByTagName(tag);
};这三行代码什么意思 新人求解::::
tag,cot 这俩参数是干什么用的 cot = cot || document; 这是干嘛? 谢谢你
tag是传入的标签名 这里是input,
cot是当前对象,默认是document,不设置就可以了
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>层次选择器</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="jquery.js"></script>
<style type='text/css'>
.foc{background-color:#d8c98d;color:#1600e1;}
</style>
<script type="text/javascript">
$(function(){
$("table input[type=text]").attr("isfocus",false);
$("table input[type=text]").bind("focus" ,function() {
$(this).addClass("foc");
$("table input[type=text]").attr("isfocus",false);
$(this).attr("isfocus",true);
}).blur(function() {
$(this).removeClass("foc");
});
var i=0;
var $focus='';
$("#btn2").click(function() {
$focus=$("input[isfocus=true]");
var m=$("input[type=text]").index($focus);
if(m>=0) {
i=i>($("input[type=text]").length-2)?0:m+1;
}
//$("input[type=text]").eq(i).trigger("focus");
$("input[type=text]")[i].focus();
});
$("input[type=text]").keyup(function(e) {
if(e.which==13) {
$("#btn2").trigger("click");
}
});
});
</script>
</head>
<br/>
<br/>
<body>
<table width="300" border="0" align="center" id="tb">
<tr>
<td>姓名</td>
<td>
<input type="text" name="txt1"/>
</td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="txt2"/></td>
</tr>
<tr>
<td>手机</td>
<td><input type="text" name="txt3"/></td>
</tr>
<tr>
<td>性别</td>
<td><input type="radio" name="radio" checked='checked'/>男 <input type="radio" name="radio"/>女</td>
</tr>
</table>
<table align="center" width="200" border="0">
<tr align="center">
<td><input type="button" value="选取 parent > child 项" id="btn1"/></td>
</tr>
<tr align="center">
<td><input type="button" value="切换焦点" id="btn2"/></td>
</tr>
</table>
</body>
</html>
用JS话,可以考虑设置一个数组,遍历需要切换焦点的元素,放到一个数组里面,设置初始值,当出现focus时,就改变数组里的值,切换时,通过判断数组里的值,来处理下一个位置。