How to detect which row [ tr ] is clicked?

前端 未结 3 1701
天涯浪人
天涯浪人 2020-12-19 14:57

In javascript, how can we detect which row of the table is clicked? At present what i am doing is, i am binding the the method at run time like this.

onload         


        
3条回答
  •  囚心锁ツ
    2020-12-19 15:24

    This uses sectionRowIndex to get the index in the containing tBody.

    function getRowIndex(e){
     e= window.event || e;
     var  sib, who= e.target || e.srcElement;
     while(who && who.nodeName!= 'TR') who= who.parentNode;
     if(who){
      alert(who.sectionRowIndex+1)
      if(e.stopPropagation) e.stopPropagation();
      else e.cancelBubble= true;
      // do something with who...
     }
    }
    
    onload= function(){
     document.getElementById('my_table').onclick= getRowIndex;
    }
    

提交回复
热议问题