How would i go abo开发者_如何学编程ut adding a class to a td tag given the following table:
<table>
<tr>
<td id="1"></td>
<td id="2"></td>
<td id="3"></td>
</tr>
</table>
I would like to add the class to the td with the id of 2. The class name in my case is td_highlight.
Have tried a few different scripts with no luck.
Thanks in advance, Billy
you can use $('#2').addClass('td_highlight');
However, using a numerical value for an id may not play nice with some browsers. According to W3c:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
You might try looking at: http://www.w3.org/TR/html4/types.html
$('#2').addClass('td_highlight');
I am not including td in the selector because all your ids (by definition) should be unique in the page.
see the relevant bit of documentation for further reference.
You can use the addClass() function:
$('#2').addClass('td_highlight');
jQuery('td#2').addClass('class');
精彩评论