On Sep 6, 12:28 pm, din <dingl...@gmail.com> wrote:
> <TR>
>     <TD>1</TD>
>     <TD>JM-53</TD>
>     <TD>--</TD>
>     <TD>--</TD>
>     <TD>--</TD>
>     <TD><A href="#">ROHs</A></TD></TR>
>   <TR>
>     <TD>2</TD>
>     <TD>JM-54</TD>
>     <TD><A href="#">ROHs</A></TD>
>     <TD>--</TD>
>     <TD>--</TD>
>     <TD><A href="#">ROHs</A></TD></TR>
>
> I want the value of "href" in first tag A is equal to "JM-53_5.htm".
> And the the value of "href" in 2nd tag A is equal to "JM-54_2.htm".
> The value of "href" in 3rd tag A is equal to "JM-54_5.htm".
> HOW can I get the current Index of the tag A,and HOW TO get the value
> of the current 2nd tag TD.

It seems that what you want to do is use the text from the 2nd cell
and the cell index of the cell that the a element is in to create an
href value for the link. Your server knows all this information before
the page is sent, why don't you do it there? Getting client-side
scripting to do this stuff just creates a dependancy where none is
required. Making this stuff dependant on the layout creates
maintenance headaches - every time the layout changes, your script
must change too.

Something like the following will do the job, however it has no
feature detection and will break if dependant parts of the layout
change:

var getText = (function() {
  var el = document.createElement('p');
  if (typeof el.textContent == 'string') {
    return function(el) {
      return el.textContent;
    }
  } else if (typeof el.innerText == 'string') {
    return function(el) {
      return el.innerText;
    }
  }
})();

function trim(s) {
  return s.replace(/(^\s*)|(\s*$)/g,'');
}

function fixLinks(id) {

  var text, a, aLinks, j;
  var row, rows = document.getElementById(id).rows;
  var i = rows.length;

  while (i--) {
    row = rows[i];
    aLinks = row.getElementsByTagName('a');
    j = aLinks.length;
    text = trim(getText(row.cells[1]));

    while (j--) {
      a = aLinks[j];
      aLinks[j].href = text + '_' + a.parentNode.cellIndex + '.htm';
    }
  }
}


--
Rob

Reply via email to