table中给TR或TD赋ID有效吗?

Is it valid to assign ID to TR or TD in table?

标题本身是不言自明的。

出于好奇,我想知道将 ID 分配给 Table 中的 TRTD 是否可行且最重要的是有效?

赞:

<td class = "xyz" id = "abc"></td>

<tr class = "xyz" id = "abc">
<td>&nbsp;</td>
</tr>

我用谷歌搜索但找不到相关答案所以我想我可以在这里问。

谢谢!

完全正确。您可以给任何 HTML 元素一个 class 或 ID。

id 属性指定 HTML 元素的唯一 ID(该值在 HTML 文档中必须是唯一的)。

id属性最常用来指向一个样式中的一个样式sheet,并通过JavaScript(通过HTMLDOM)来操作元素有特定的id。

来源:w3schools

id 用于引用 css 文件或 javascript 中的 html 控件。您需要确保没有两个 id 匹配,即使它没有被禁止但会导致预期的结果。

在css中:

<style>
  #myid{
    color:red;
  }
</style>

在JQuery中:

<script>
    alert($("#myid").name;
</script>

更新:修复了使用 class 选择器代替 id 选择器的错误。

您可以使用 classid 定位您的 <td>,就像通过 CSS 定位任何其他元素一样。它基于 W3C 标准完全有效。

是的,在这些元素上使用 class and/or id 是有效的。您可以将它们应用于所有 HTML 元素。有关详细信息,请参阅

https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes

如你所见class和id是全局属性

Global attributes are attributes common to all HTML elements; they can be used on all elements, though the attributes may have no effect on some elements.

是的。

来自the HTML 5 specification

The following attributes are common to and may be specified on all HTML elements (even those not defined in this specification):

  • id