---
title: "js根据属性查找元素代码"
author: "floydd"
date: "2007-09-16T09:39:00Z"
tags: []
slug: "js"
draft: false
lastmod: "2011-12-26T12:03:28Z"
id: 1326395
url: "https://floydd.iteye.com/blog/1326395"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)functiongetElementByAttribute(aAttribute,aValue,aInElement)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)varElementVerifier;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)varElements=newArray();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)functionSearchElement(aElement)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(aElement==null||aElement==undefined)return  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(ElementVerifier(aElement))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Elements\[Elements.length]=aElement;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SearchElement(aElement.firstChild);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SearchElement(aElement.nextSibling);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(aInElement==undefined)aInElement=document.body;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)ElementVerifier=function(aElement)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)Element=aElement;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(aElement.nodeName=='#text')returnfalse;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)functionmyEqual(Element,aValue)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)vareq=false;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)if(!(eq=(Element.getAttribute(aAttribute)==aValue)))  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)...{  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)eval('try{eq=(Element.'+aAttribute+'=="'+aValue+'");}catch(e){}');  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returneq;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnmyEqual(Element,aValue);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)}  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)SearchElement(aInElement);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)returnElements;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)}

似乎查找的效率有点低