首页 > 代码库 > getElementsByTagName()

getElementsByTagName()

 window.onload = function(){              var oUL1 = document.getElementById("ul1");              var oUL2 = document.getElementById("ul2");              var oDiv = document.getElementById("div1");                                  oLi = getElementsByClassName(oDiv,‘p‘, ‘box‘);                            for(var i=0; i< oLi.length; i++)              {                  oLi[i].style.background= "red";              }        }                function getElementsByClassName(parent, tagName, className)        {                 var aEls = parent.getElementsByTagName(tagName);                 var aArr = [];                              for(var i=0; i< aEls.length; i++)                 {                                                            var aClass = aEls[i].className.split(" ");                                          for(var j=0; j< aClass.length; j++)                        {                              if(aClass[j] == className)                              {                                  aArr.push(aEls[i]);                                  break;                              }                                                 }                                   }                             return aArr;        }
 1 <ul id="ul1"> 2     <li>111111111</li> 3     <li class="box box">222222222</li> 4     <li>333333333</li> 5     <li class="box box1">444444444</li> 6 </ul> 7  8  9 <ul id="ul2">10     <li>111111111</li>11     <li class="box box">222222222</li>12     <li>333333333</li>13     <li class="box box1">444444444</li>14 </ul>15 16 <div id="div1">17     <p class="box">11111111</p>18 </div>