开发者

Selecting a TD element from it's ID in jQuery

开发者 https://www.devze.com 2023-01-05 12:49 出处:网络
I have an array called currentTD that holds two integers, the first element in the array is an ID to a <TR> element, and the second element in the array is the ID of a <TD> element within

I have an array called currentTD that holds two integers, the first element in the array is an ID to a <TR> element, and the second element in the array is the ID of a <TD> element within that <TR>.

I want to somehow select that <TD> and change its background colour. I'm just not sure how I can select that <TD>开发者_StackOverflow中文版 given the "co-ordinates" from currentTD?

Thanks.


This is based on your previous question: you can use .eq(), like this:

$("table").find("tr").eq(trIndex).children("td").eq(tdIndex)
          .css({ backgroundColor: "red" });

This assumes you have an array like this: [tdIndex, trIndex]. As for your previous markup, remove those IDs, they're both invalid and not needed.

IDs can't start with a number (unless you're using HTML5) and can't be repeated, as they are on your <td>s. But since you can get what you want via indexes...there's no need for the id attributes anyway, so just remove them.


If currentTD has an id of the TD to change it's background as it's second element, then the following should work.

$('#' + currentTD[1]).css('background-color', 'red');


The Id of an element is unique, so you can select using the Id of the <td> directly:

$('#' + currentTD[1]).css('background-color', color);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号