日期:2014-05-16  浏览次数:20468 次

关于JAVASCRIPT的模糊查找的问题,高手请进
一个页面中有好多程序生成的元素,他们的ID类似这样:id= "1_2 ",   id= "2_3 ",id= "10_2 ",   id= "3_4 "……

如何能得到ID的第一个字母都是1的元素的ID呢?

谢谢~~

------解决方案--------------------
<!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 ">
<SCRIPT language= "javascript ">
<!--
function test(){
var objs = document.getElementsByTagName( "* ")//  *  这里是重点
for(var i = 0 ; i <objs.length ;i++ ){
if(objs[i].id && objs[i].id.indexOf( "1 ") == 0){
alert(objs[i].id);
}
}
}

-->
</SCRIPT>
<body>
<div id= "1_3 "> fdsafdsaf </div>
<span id= "2_1 "> fdasfdfdsa </span>

<lable id= "1_23 "> dddd </lable>
<input type= "text " id= "1_2 " value= "test " />
<input type= "checkbox " id= "1_4 " value= "test " />
<input type= "radio " id= "1_5 " value= "test " />
<input type= "hidden " id= "1_6 " value= "test " />
<input type= "button " id= "10_7 " value= "clickHere " onclick= "test(); "/>
</body>
------解决方案--------------------
LS的正解
除了JF,米事干
------解决方案--------------------
建議使用正則表達式