jQuery - 如何将类添加到td标记

Bil*_*gan 3 css jquery

如何在给定下表的情况下向td标记添加类:

<table>
    <tr>
        <td id="1"></td>
        <td id="2"></td>
        <td id="3"></td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

我想将类添加到id为2的td.在我的例子中,类名是td_highlight.

尝试了几个不同的脚本没有运气.

先谢谢,比利

edl*_*edl 11

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


Joh*_*dol 5

$('#2').addClass('td_highlight');
Run Code Online (Sandbox Code Playgroud)

我没有在选择器中包含 td ,因为您的所有 ID(根据定义)在页面中都应该是唯一的。

请参阅相关文档以获取进一步参考。