急! 如何取得当前页面上自定义属性controltype的值='aa'的所有元素集合?
up 问题点数:30、回复次数:2Top
1 楼meizz(梅花雪)回复于 2005-04-04 12:58:28 得分 15
<SCRIPT LANGUAGE="JavaScript">
var a = document.all;
var len = a.length;
var b = [];
for (var i=0; i<len; i++)
{
if (a[i].controltype=="aa") b[b.length] = a[i];
}
alert(b.length);
</SCRIPT>Top
2 楼faisun(暖阳)回复于 2005-04-04 13:08:14 得分 15
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body controltype="aa"><a href="#" controltype="aa">11</a>
<script>
elementsArray=new Array();
function getElementsArray(object){
var i;
var len=object.childNodes.length;
var objectchild;
for(i=0;i<len;i++){ //在子节点中遍历
objectchild=object.childNodes[i];
if(typeof(objectchild.controltype)!="undefined"){
if(objectchild.controltype=="aa"){ //找到元素,加入elementsArray
elementsArray.push(objectchild);
alert(objectchild.outerHTML);
}
}
if(object.childNodes.length>0){ //有子节点,递归查找
getElementsArray(objectchild);
}
}
}
getElementsArray(window.document);
//得到的元素保存在 elementsArray 中
</script>
</body>
</html>Top




