JS获取table的行号,并根据行数显示隐藏

JavaScript024

JS获取table的行号,并根据行数显示隐藏,第1张

$("table tr").each(function(i){

    console.log(i)

})

----根据行数自动循环显示隐藏

<style>

   tr:nth-child(odd){

      height:70px

      background-color:#0f9

   }

   tr:nth-child(even){

      display:none

      height:60px

      background-color:#c3c

   }

</style>

<script>

window.onload=function(){

   var trs=document.getElementsByTagName("tr")

   for(var i=0i<trs.lengthi+=2){

      (function(i){

         trs[i].onclick=function(){

            for(var j=0j<trs.lengthj+=2){

               if(j==i){

                  if(trs[j+1].style.display!="table-row"){

                     trs[j+1].style.display="table-row"

                  }else{

                     trs[j+1].style.display="none"

                  }

               }else{

                  trs[j+1].style.display="none"

               }

            }

         }

      })(i)

   }

}

</script>

<table>

<tr><td>点击本行</td></tr>

<tr><td>1</td></tr>

<tr><td>点击本行</td></tr>

<tr><td>2</td></tr>

<tr><td>点击本行</td></tr>

<tr><td>3</td></tr>

<tr><td>点击本行</td></tr>

<tr><td>4</td></tr>

<tr><td>点击本行</td></tr>

<tr><td>5</td></tr>

<tr><td>点击本行</td></tr>

<tr><td>6</td></tr>

</table>