Open-Source World

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

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

          代碼
            為prototype的Element增加兩個(gè)方法
          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.                    //這個(gè)判斷我改成了!=null原來(lái)沒(méi)有但這樣當(dāng)你在元素中只是添加了某個(gè)屬性   
          5.                       //如<input type='text' required />這時(shí)原來(lái)的代碼就會(huì)找不到   
          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. });   



          使用時(shí)

          代碼
          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>  





           


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 永新县| 贵定县| 邯郸县| 平远县| 许昌市| 崇左市| 石泉县| 麻江县| 天等县| 丰县| 赞皇县| 麻栗坡县| 万山特区| 大丰市| 永靖县| 苍溪县| 丽水市| 山西省| 获嘉县| 塔城市| 新乡市| 彭山县| 鸡泽县| 静安区| 渝北区| 万年县| 巴东县| 同心县| 仙游县| 象山县| 万源市| 仁怀市| 洞口县| 丽江市| 汉阴县| 炎陵县| 翼城县| 朝阳县| 大庆市| 温宿县| 广东省|