[jQuery] Re: How can I get current Index?

2009-09-08 Thread Ricardo

See the eq() and index() methods:
http://docs.jquery.com/Selectors/eq#index
http://docs.jquery.com/Core/index#subject

$('#xxx tr').each(function(){
   var
items = $(this).children(),
title = items.eq(1).text();

   items.each(function(){
  $(this).find('a').attr('href', title + '_' + items.index
( this ));
   });

});

On Sep 5, 11:28 pm, din dingl...@gmail.com wrote:
 TR
     TD1/TD
     TDJM-53/TD
     TD--/TD
     TD--/TD
     TD--/TD
     TDA href=#ROHs/A/TD/TR
   TR
     TD2/TD
     TDJM-54/TD
     TDA href=#ROHs/A/TD
     TD--/TD
     TD--/TD
     TDA 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.

 MANY MANY THANKS


[jQuery] Re: How can I get current Index?

2009-09-06 Thread Jules

This works, but there could be a more elegant way to do it.

$(document).ready(function() {
var cnt;

$(tr).each(function() {
cnt = 0;
$(this).find(td).each(function() {
$(this).find(a).each(function() { alert(cnt) });
cnt++;

});
});
});

On Sep 6, 12:28 pm, din dingl...@gmail.com wrote:
 TR
     TD1/TD
     TDJM-53/TD
     TD--/TD
     TD--/TD
     TD--/TD
     TDA href=#ROHs/A/TD/TR
   TR
     TD2/TD
     TDJM-54/TD
     TDA href=#ROHs/A/TD
     TD--/TD
     TD--/TD
     TDA 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.

 MANY MANY THANKS


[jQuery] Re: How can I get current Index?

2009-09-06 Thread RobG



On Sep 6, 12:28 pm, din dingl...@gmail.com wrote:
 TR
 TD1/TD
 TDJM-53/TD
 TD--/TD
 TD--/TD
 TD--/TD
 TDA href=#ROHs/A/TD/TR
   TR
 TD2/TD
 TDJM-54/TD
 TDA href=#ROHs/A/TD
 TD--/TD
 TD--/TD
 TDA 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