var t = document.getElementById('a'+v.value);
这里加个判断就可以了
if (!t) {你的处理;
return 0;
}
或者结合上下内容写成:
var t = document.getElementById('a'+v.value);
if(t){
t.scrollIntoView();
window.focus();
}
else {你的处理}
这里加个判断就可以了
if (!t) {你的处理;
return 0;
}
或者结合上下内容写成:
var t = document.getElementById('a'+v.value);
if(t){
t.scrollIntoView();
window.focus();
}
else {你的处理}
<script language="javascript">
function s(){
var v = document.getElementById('text');
var t = document.getElementById('a'+v.value);
if (!t) t=document.getElementById('text');
}
function b(t){
t.scrollIntoView();
window.focus();
}
</script>
<body onload="b(document.getElementById('text'));">
<script language="javascript">
function s(){
var v = document.getElementById('text');
var t = document.getElementById('a'+v.value);
if (!t) t=document.getElementById('text');
b(t);//昨天回复时少写了这句啊,太急了,现在的效果是如果没有就停在输入框那里了。
}
function b(t){
t.scrollIntoView();
window.focus();
}
</script>
<body onload="b(document.getElementById('text'));">
<!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>
</head>
<script language="javascript">
function s(){
var v = document.getElementById('text');
if (/^[1-9]$/.test(v.value)==false){
alert('必须填入1-9的数字');
v.focus();
return false;
}
var t = document.getElementById('a'+v.value);
t.scrollIntoView();
window.focus();
}
</script>
<body onload="document.getElementById('text').focus();">
<p>输入1~9的单个数字:
<form onsubmit="s();return false">
<input type="text" id="text" value="" />
<input type="submit" value="提交"/>
</form>
</p>
<p><a href="#6">111111111111111</a></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>
<a id="a1">1</a></p>
<p><br />
<a id="a2">2</a></p>
<p><br />
<a id="a3">3</a></p>
<p><br />
<a id="a4">4</a></p>
<p><br />
<a id="a5">5</a></p>
<p><br />
<a id="a6">6</a></p>
<p><br />
<a id="a7">7</a></p>
<p><br />
<a id="a8">8</a></p>
<p><br />
<a id="a9">9</a><br />
</p>
<div style="height:3000px;">...</div>
</body>
</html>
<!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>
</head>
<script language="javascript">
function s(){
var v = document.getElementById('text');
if (/^[1-9]$/.test(v.value)==false){
alert('必须填入1-9的数字');
v.focus();
return false;
}
var t = document.getElementById('a'+v.value);
t.scrollIntoView();
window.focus();
}
</script>
<body onload="document.getElementById('text').focus();">
<p>输入1~9的单个数字:
<form onsubmit="s();return false">
<input type="text" id="text" value="" />
<input type="submit" value="提交"/>
</form>
</p>
<p><a href="#6">111111111111111</a></p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p>
<a id="a1">1</a></p>
<p><br />
<a id="a2">2</a></p>
<p><br />
<a id="a3">3</a></p>
<p><br />
<a id="a4">4</a></p>
<p><br />
<a id="a5">5</a></p>
<p><br />
<a id="a6">6</a></p>
<p><br />
<a id="a7">7</a></p>
<p><br />
<a id="a8">8</a></p>
<p><br />
<a id="a9">9</a><br />
</p>
<div style="height:3000px;">...</div>
</body>
</html>