<!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>
<title>无标题页</title>
</head>
<body>
<input type="text" id="s1">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<script>
var i=0;
document.getElementById("s1").onkeyup=function()
{var a=/^\d+$/g;
var b=/[^\d]/g;
this.value=this.value.replace(b,"");
if(this.value.match(a)!=null)
{var div =document.getElementsByTagName("div");
div[i].style.background="red";
i++;
}
}
</script>
</body>
</html>
<head>
<title>无标题页</title>
</head>
<body>
<input type="text" id="s1">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<script>
var i=0;
document.getElementById("s1").onkeyup=function()
{var a=/^\d+$/g;
var b=/[^\d]/g;
this.value=this.value.replace(b,"");
if(this.value.match(a)!=null)
{var div =document.getElementsByTagName("div");
div[i].style.background="red";
i++;
}
}
</script>
</body>
</html>
如果用的样式
就div[i].style.background="red";
gaicheng div[i].tagName=“你要的名字”
比如有名字
<head>
<title>无标题页</title>
<style>
.a{ background:red;}
.b{background:green;}
.c{background:yellow;}
.d{background:blue;}
.e{background:sliver;}
</style>
</head>
<body>
<input type="text" id="s1">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<script type="text/javascript">
var o=null;
var div =document.getElementsByTagName("div");
document.getElementById("s1").onkeyup=function()
{
var str=new Array('red','green','yellow','blue','sliver');
var a=/\d/g;
if(this.value.match(a)!=null)
{var length=this.value.match(a).length;
if(o>length)
{
div[o-1].style.background="";
}
o=length;
for(var c=0;c<length;c++)
{
div[c].style.background =str[c];
}}
}
</script>
</body>
</html>
<head>
<title>无标题页</title>
<style>
.a{ background:red;}
.b{background:green;}
.c{background:yellow;}
.d{background:blue;}
.e{background:sliver;}
</style>
</head>
<body>
<input type="text" id="s1">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<script type="text/javascript">
var o=null;
var div =document.getElementsByTagName("div");
document.getElementById("s1").onkeyup=function()
{
var str=new Array('red','green','yellow','blue');
var a=/\d/g;
if(this.value=="")
{
div[0].style.background="";
}
if(this.value.match(a)!=null)
{var length=this.value.match(a).length;
if(o>length)
{
div[o-1].style.background="";
}
o=length;
for(var c=0;c<length;c++)
{
if(c<4)
{
div[c].style.background =str[c];
}
else
{
break;
}
}
}
}
</script>
</body>
</html>jf
<div id="dv1" ></div>
<div id="dv2" > </div>
<div id="dv3" > </div>
<div id="dv4" > </div>
<div id="dv5" ></div>
<div id="dv6" ></div>
<div id="dv7" ></div>
<div id="dv8" ></div>
<div id="dv9" ></div>
<div id="dv10" ></div>
<div id="dv11" ></div>
<div id="dv12" ></div>
<div id="dv13" ></div>
<div id="dv14" ></div>
<div id="dv15" ></div>
<div id="dv16" ></div>
<div id="dv17" ></div>
<div id="dv18" ></div>for (i=0;i<s1.value.length;i++)
{
dv[i].style.background="red";
}这样为什么不行?
<!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>
<title>无标题页</title>
</head>
<body>
<input type="text" id="s1" />
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<script>
var div=document.getElementsByTagName("div");
document.getElementById("s1").onkeyup=function()
{
for(var i=0;i<this.value;i++)
{
div[i].style.background="red";
}
}
</script>
</body>
</html>删除原理其实一样的
好像不是很好吧。
用document.getElementsByName
给 div 取一样的name
<!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>
<title>无标题页</title>
</head>
<body>
<input type="text" id="s1" />
<div name="a">1</div>
<div name="a">2</div>
<div name="a">3</div>
<div name="a">4</div>
<div name="a">5</div>
<script>
var div=document.getElementsByName ("a");
document.getElementById("s1").onkeyup=function()
{
for(var i=0;i<this.value;i++)
{
div[i].style.background="red";
}
}
</script>
</body>
</html>
获取不到值啊
<body>
<input type="text" id="s1">
<div name="m">1</div>
<div name="m">2</div>
<div name="m">3</div>
<div name="m">4</div>
<div name="m">5</div>
<div name="m">6</div>
<div name="m">7</div>
<script>
var i=0;
document.getElementById("s1").onkeyup=function()
{var a=/^\d+$/g;
var b=/[^\d]/g;
this.value=this.value.replace(b,"");
if(this.value.match(a)!=null)
{var div =document.getElementsByName("m");
div[i].style.background="red";
i++;
}
}
</script>
</body>
不是name 是id=“a”
用class 不行 id也不行
<script>
alert(document.getElementsByName("a1").length)
</script>
getElementByTagName
错了
getElementsByTagName
var div =document.getElementsByName("m");
div[i].style.background="";
i++;
最少要多给我50分
window.event.keyCode==37;
event.returnvalue=false;