Open-Source World

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

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

          代碼
            為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ā)表評論。


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 平乡县| 门源| 平邑县| 麟游县| 武穴市| 若羌县| 无为县| 厦门市| 绥滨县| 措美县| 沐川县| 淮北市| 大姚县| 浙江省| 镇康县| 瑞金市| 钟祥市| 阜城县| 含山县| 电白县| 星座| 遵化市| 明溪县| 哈尔滨市| 白河县| 黄梅县| 湖口县| 广饶县| 启东市| 霍州市| 镇原县| 双流县| 郑州市| 巨鹿县| 射洪县| 苗栗市| 凭祥市| 象山县| 武汉市| 宁蒗| 金坛市|