Sunday 15 September 2013

javascript - How to detect which row [ tr ] is clicked? -


In JavaScript, how can we find out which row the table is clicked on? What I am currently doing, I am complying with the method of running this way.

  onload = function (if (! Document.getElementsByTagName ||! Document.createTextNode) Return; Var rows = document. GetElementById ('my_table') GetElementsByTagName ('Toby') [ 0] .getElementsByTagName ('tr'); (I = 0; i & lt; rows.length; i ++) {rows [i] .onclick = function () {Warning (this.rowIndex + 1); }}  

[copied []]

But I do not like this view. Is there a choice? My problem is only to get an indicator of that line

  • Do not please any jQuery: D.

In fact, you add a click handler to your table. This handler reads the tag of the clicked element and takes the dot tree upwards. If that line is not found, if one row is found, then it works on it and returns. Some such (has not been tested yet, but you can give the idea):

  var table = document.getElementB YId ('my_table'); Table. Click on = function (e) {E = E || accident; Var Event EL = ESRC Element || E.target, parent = event alpherate node, isRow = function (L) {returns el.tagName.match (/ tr / i)); }; // The DOM can be reached till reaching the top (parent = parent.parentNode) {If (IRRO (original)) {// row meets, do something with it and return, e.g. Warning (parent.rowIndex + 1); Back true; }        }        return false; };  

No comments:

Post a Comment