2013-10-11 2 views
0

특정 셀을 클릭 할 때 셀 인덱스를 가져올 수 있도록 테이블의 셀에 리스너를 추가하고 있습니다. 그러나 나는 크롬에서 행 색인의 가치를 얻을 수 없습니다. 이것은 IE10과 Firefox에서 잘 작동합니다. 코드는 다음과 같습니다.parentNode.rowIndex가 크롬에서 작동하지 않습니다.

function AttachEvents() { 
    var cls = document.getElementById("TableContents").getElementsByTagName("td"); 

    for (var i = 0; i < cls.length; i++) { 
     if (cls[i].addEventListener) { 
      cls[i].addEventListener("click", alertRowCell, false); 
     } else if (cls[i].attachEvent) { 
      cls[i].attachEvent("onclick", alertRowCell); 
     } 
    } 
} 

function alertRowCell(e) { 
    var cell = e.target || window.event.srcElement; 
    //alert(cell.cellIndex + ' : ' + cell.parentNode.rowIndex); 
    if (cell.cellIndex == 1) { 
     alert(" The row index is " + cell.parentNode.rowIndex); 
     highlight(); 
    } 
} 

Chrome에서 어떻게 해결할 수 있습니까?

+0

오류 :

이 예제를 시도

? Chrome에서'cell.parentNode'는 어떤 요소입니까? – alex

+0

어떻게 작동하지 않는지 설명해 주시겠습니까? 그래서 우리는 스스로를 찾아야하지 않습니까? –

+0

크롬을 제외하고 cell.parentNode.Rowindex는 클릭 된 실제 행을 반환합니다. 크롬에서 -1을 반환합니다. –

답변

3

this으로 클릭 한 요소를 쉽게 참조 할 수 있습니다. 당신의 경우 에서

은 ... 다음 e.target 누구 spanparentNode 나는 당신의 테이블 구조 인 경우 때문입니다 말인지 이유 ...

<tr> 
    <td><span>Hello</span></td> 
</tr> 

function alertRowCell(evt) { 
    var tr = this.parentNode; 
    // do something with tr... 
} 
입니다

td이고 tr이 아님. http://jsfiddle.net/naJBq/

관련 문제