Html 为表中的TR或TD分配ID是否有效?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29256197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it valid to assign ID to TR or TD in table?
提问by Quentin
Title itself is self explanatory.
标题本身是不言自明的。
Just as a curiosity I want to know if its possible and most importantly valid to assign ID to TR
or TD
in Table?
出于好奇,我想知道将 ID 分配给表TR
或TD
在表中分配 ID 是否可能且最重要的是有效?
Like :
喜欢 :
<td class = "xyz" id = "abc"></td>
or
或者
<tr class = "xyz" id = "abc">
<td> </td>
</tr>
I googled it but couldn't find relevant answer so I thought I could ask here.
我用谷歌搜索但找不到相关答案所以我想我可以在这里问。
Thanks !
谢谢 !
回答by Quentin
Yes.
是的。
The following attributes are common to and may be specified on all HTML elements(even those not defined in this specification):
…
- id
…
以下属性是所有 HTML 元素(甚至是本规范中未定义的元素)的通用属性,并且可以在其上指定:
…
- ID
…
回答by Huangism
Yes it is valid to have class and/or id on those elements. You can apply them to all HTML elements. For more info see
是的,在这些元素上拥有 class 和/或 id 是有效的。您可以将它们应用于所有 HTML 元素。有关更多信息,请参阅
https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
As you can see class and id are Global 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.
全局属性是所有 HTML 元素共有的属性;它们可以用于所有元素,尽管属性可能对某些元素没有影响。
回答by Jasper
That is completely valid. You can give any HTML-element a class or ID.
这是完全有效的。你可以给任何 HTML 元素一个类或 ID。
The id attribute specifies a unique id for an HTML element (the value must be unique within the HTML document).
id 属性为 HTML 元素指定唯一的 id(该值在 HTML 文档中必须是唯一的)。
The id attribute is most used to point to a style in a style sheet, and by JavaScript (via the HTML DOM) to manipulate the element with the specific id.
id 属性最常用于指向样式表中的样式,并且通过 JavaScript(通过 HTML DOM)来操作具有特定 id 的元素。
Source: w3schools
资料来源:w3schools
回答by nizam
The id is used to refer the html control in css file or javascript. You need to ensure that no two id are matching even though it is not prohibited but will lead to enexpected result.
id 用于引用 css 文件或 javascript 中的 html 控件。您需要确保没有两个 id 匹配,即使它没有被禁止,但会导致预期的结果。
in css:
在 CSS 中:
<style>
#myid{
color:red;
}
</style>
in JQuery:
在 JQuery 中:
<script>
alert($("#myid").name;
</script>
update: fixed error of using class selector in place of id selector.
更新:修复了使用类选择器代替 id 选择器的错误。
回答by Nima
You can target your <td>
using class
or id
just like any other elements via CSS. It is perfectly valid based on W3C standards.
您可以通过 CSS定位您的<td>
使用class
或id
任何其他元素。基于 W3C 标准,它是完全有效的。