Open-Source World

          let's learn and study.
          posts - 28, comments - 23, trackbacks - 0, articles - 1

          一下的代碼是我從國外的網上看的,我又改進了一點,在這跟大家分享

          代碼
            為prototype的Element增加兩個方法
          1. document.getElementsByAttribute = function(attribute,parent) {   
          2.     return $A(($(parent) || document.body).getElementsByTagName('*')).inject([],function(elements,child){   
          3.         if(Element.readAttribute(child,attribute)!=null)   
          4.                    //這個判斷我改成了!=null原來沒有但這樣當你在元素中只是添加了某個屬性   
          5.                       //如<input type='text' required />這時原來的代碼就會找不到   
          6.             elements.push(Element.extend(child));   
          7.         return elements;   
          8.     });   
          9. }   
          10.   
          11. document.getElementsByAttributeValue = function(attribute,value,parent) {   
          12.     return $A(($(parent) || document.body).getElementsByTagName('*')).inject([],function(elements,child){   
          13.         if(Element.readAttribute(child,attribute) == value)   
          14.             elements.push(Element.extend(child));   
          15.         return elements;   
          16.     });   
          17. }   
          18.   
          19. Element.addMethods({   
          20.     getElementsByAttribute: function(element,attribute){   
          21.         return document.getElementsByAttribute(attribute,element);   
          22.     },   
          23.     getElementsByAttributeValue: function(element,attribute,value){   
          24.         return document.getElementsByAttributeValue(attribute,value,element);   
          25.     }   
          26. });   



          使用時

          代碼
          1. <html>  
          2. <head>  
          3. <script src='prototype.js'></script>  
          4. <script src='prototype.tidbits.js'></script>  
          5. <script language="javascript" type="text/javascript">  
          6.     Event.observe(window,'load',function(){   
          7.         alert($('div1').getElementsByAttribute('require').length);   
          8.         alert(document.getElementsByAttribute('require').length);   
          9.     })   
          10. </script>  
          11. </head>  
          12. <body>  
          13.     <div id='div1'>  
          14.     <input type='text'   require/>  
          15.     <input type='text' require />  
          16.     </div>  
          17. </body>  
          18. </html>  





           


          只有注冊用戶登錄后才能發(fā)表評論。


          網站導航:
           
          主站蜘蛛池模板: 镶黄旗| 山西省| 曲周县| 塔河县| 水城县| 高邮市| 通江县| 社会| 罗源县| 阳西县| 晋宁县| 元朗区| 新密市| 巴南区| 思南县| 盐亭县| 安顺市| 渝中区| 绵竹市| 龙海市| 乌拉特前旗| 安化县| 金坛市| 大厂| 冕宁县| 辽源市| 马山县| 方山县| 沅江市| 新源县| 顺昌县| 什邡市| 漾濞| 丹阳市| 宁阳县| 浏阳市| 云安县| 凭祥市| 肇庆市| 商水县| 津市市|