js经验不足,做到一半不知道如何继续了。。。

nattystyle 2008-02-11 11:09:03

<!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 type="text/javascript">
function myclick()
{
alert("随便去做一些事情");
var mybutton=event.srcElement;
mybutton.focus();
}
function aaa(){
var mybutton=event.srcElement;
var btnid = mybutton.id;
alert(btnid);
if(event.keyCode==39){
button3.focus();
}
if(event.keyCode==37){
button1.focus();
}
}
</script>
</head>

<body>
<input id="button1" type="button" value="button1" />
<input id="button2" type="button" value="button2" onclick="myclick();" onkeydown="aaa()"/>
<input id="button3" type="button" value="button3" />
</body>
</html>


想要实现用键盘的左右键控制按钮的焦点,而且按钮不止这三个,可能有10多个。所以不能象这样写死,要按下方向键的时候取出id,然后将这个id++或者id--,在id.focus();

但不知道如何实现,请各位帮忙看一下,谢谢
...全文
81 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuyann 2008-02-11
  • 打赏
  • 举报
回复
<html> 
<head>
<title>U</title>
<script type="text/javascript">
function myclick()
{
alert("随便去做一些事情");
var mybutton=event.srcElement;
mybutton.focus();
}

function aaa(){
var nMaxID = 3;
var mybutton=event.srcElement;
var btnid = mybutton.id;
var nID = parseInt(btnid.substring("button".length))

if(event.keyCode==39){
nID++;
}
if(event.keyCode==37){
nID--;
}

if (nID>nMaxID)
nID = 1;

if (nID<1)
nID = nMaxID;

document.getElementById("button"+nID).focus();
}
</script>
</head>

<body>
<input id="button1" type="button" value="button1" onclick="myclick();" onkeydown="aaa()" />
<input id="button2" type="button" value="button2" onclick="myclick();" onkeydown="aaa()"/>
<input id="button3" type="button" value="button3" onclick="myclick();" onkeydown="aaa()" />
</body>
</html>
myvicy 2008-02-11
  • 打赏
  • 举报
回复
<!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 type="text/javascript">
function myclick()
{
alert("随便去做一些事情");
var mybutton=event.srcElement;
mybutton.focus();
}
function aaa(){
alert(this.id);
if(event.keyCode==39){
button3.focus();
}
if(event.keyCode==37){
button1.focus();
}
}
</script>
</head>

<body>
<input id="button1" type="button" value="button1" onkeydown="aaa(this)" />
<input id="button2" type="button" value="button2" onclick="myclick();" onkeydown="aaa(this)"/>
<input id="button3" type="button" value="button3" onkeydown="aaa(this)" />
</body>
</html>
xiaojing7 2008-02-11
  • 打赏
  • 举报
回复
?

87,994

社区成员

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

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