Posted on 2007-09-05 10:46
tearofmoscow 阅读(289)
评论(0) 编辑 收藏
一下的代码是我从国外的网上看的,我又改进了一点,在这跟大家分享
代码
为prototype的Element增加两个方法
- document.getElementsByAttribute = function(attribute,parent) {
- return $A(($(parent) || document.body).getElementsByTagName('*')).inject([],function(elements,child){
- if(Element.readAttribute(child,attribute)!=null)
-
-
- elements.push(Element.extend(child));
- return elements;
- });
- }
-
- document.getElementsByAttributeValue = function(attribute,value,parent) {
- return $A(($(parent) || document.body).getElementsByTagName('*')).inject([],function(elements,child){
- if(Element.readAttribute(child,attribute) == value)
- elements.push(Element.extend(child));
- return elements;
- });
- }
-
- Element.addMethods({
- getElementsByAttribute: function(element,attribute){
- return document.getElementsByAttribute(attribute,element);
- },
- getElementsByAttributeValue: function(element,attribute,value){
- return document.getElementsByAttributeValue(attribute,value,element);
- }
- });
使用时
代码
- <html>
- <head>
- <script src='prototype.js'></script>
- <script src='prototype.tidbits.js'></script>
- <script language="javascript" type="text/javascript">
- Event.observe(window,'load',function(){
- alert($('div1').getElementsByAttribute('require').length);
- alert(document.getElementsByAttribute('require').length);
- })
- </script>
- </head>
- <body>
- <div id='div1'>
- <input type='text' require/>
- <input type='text' require />
- </div>
- </body>
- </html>