Html <p> 标签可以使用 id 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4520092/
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 okay to use an id for <p> tags?
提问by Jenna
My coworker is telling me I shouldn't use id's for paragraph tags.. I think its the way to go if you know your only using that kind of paragraph once on the page.
我的同事告诉我,我不应该将 id 用于段落标签。如果您知道自己只在页面上使用过一次那种段落,我认为这是要走的路。
He also says that all elements on a page should only use class and not id, unless you are defining a header, container, or footer.
他还说页面上的所有元素都应该只使用 class 而不是 id,除非您定义了页眉、容器或页脚。
I am fresh out of college and I learned to use id for things that will only show once on a page, while using class for things that will show multiple times on a page.
我刚从大学毕业,我学会了将 id 用于只会在页面上显示一次的内容,而将 class 用于将在页面上多次显示的内容。
So, I would like to know what way is proper since I am new to the job and want to do it correctly.
所以,我想知道什么方法是正确的,因为我是这份工作的新手并且想要正确地做。
Thanks
谢谢
采纳答案by Dave Downs
I'd say it's fine as long as you know that the paragraph will only be used once on the page.
我会说只要您知道该段落只会在页面上使用一次就可以了。
An example might be a piece of company info that you want to appear on multiple pages but be styled in a particular way. Giving that an id singles it out as unique and allows you to style it as such.
一个示例可能是您希望出现在多个页面上但以特定方式设置样式的公司信息。给出一个 id 将它挑出来作为唯一的,并允许你这样设计它。
The class attribute should be used for styling a number of controls in a similar way (i.e. all those that belong to that class). For example, report totals might always need to be large and bold, so the encompassing tags would be given a reportTotals
class. There might be more than one report and more than one total per page, but they should all look the same.
class 属性应该用于以类似的方式设置多个控件的样式(即属于该类的所有控件)。例如,报表总计可能总是需要大而粗,因此包含的标签将被赋予一个reportTotals
类。可能有不止一份报告,每页有不止一份报告,但它们看起来应该都一样。
回答by Pablo Santa Cruz
Yes, it's OK. Every DOM member can have an ID. There is nothing fundamentally wrong with a <p>
tag using an ID.
好的,可以。每个 DOM 成员都可以有一个 ID。<p>
使用 ID的标签从根本上没有错。
Now, for applying CSS may be the case that is better for you using a class
attribute instead of an id
in <p>
case because you might want to apply that style to several paragraphs. But it's just a matter of convenience.
现在,应用CSS可以使用是更好地为您的情况class
属性,而不是一个id
在<p>
情况下,因为你可能需要在该样式应用于多个段落。但这只是一个方便的问题。
回答by Mark Byers
Use an id
to refer to a specific element. Use class
to refer to all elements of a specific type.
使用 anid
来引用特定元素。用class
指特定类型的所有元素。
I think [you should use id on a paragraph tag] if you know you're only using that kind of paragraph once on the page.
我认为 [你应该在段落标签上使用 id] 如果你知道你只在页面上使用那种段落一次。
You shouldn't use id
just because you only happen have one of them on your page. You should use id
when you want to be sure to only affect that one specific element, both now and in the future when other people add more paragraphs. If you have a specific "kind" of paragraph there is nothing wrong with using a class
to represent this, even if that class currently only has one member.
您不应该id
仅仅因为您的页面上只有其中一个而使用。id
当您希望确保现在和将来其他人添加更多段落时仅影响该特定元素时,您应该使用。如果您有特定的“种类”段落,则使用 aclass
表示这一点没有错,即使该类当前只有一个成员。
回答by McStretch
From a semantic point of view, I would say that if the ids help split up the document structurally then using ids makes a lot of sense. For instance, you may have:
从语义的角度来看,我会说如果 ids 有助于在结构上拆分文档,那么使用 ids 就很有意义。例如,您可能有:
<p id="beginParagraph" ...> </p> and <p id="endParagraph" ...> </p>
<p id="beginParagraph" ...> </p> and <p id="endParagraph" ...> </p>
that help easily identify and locate your beginning and ending paragraphs. Keep in mind that you should not have duplicate ids however, and the example above could easily get out of hand if you have many paragraphs and wanted to add an id for each.
这有助于轻松识别和定位您的开头和结尾段落。请记住,您不应该有重复的 id,如果您有很多段落并想为每个段落添加一个 id,则上面的示例很容易失控。
Check out this article on Classes on Ids for other reasons why one might be better over another:
查看这篇关于 Id 类的文章,了解为什么一个人可能比另一个人更好的其他原因:
回答by markyph
I find the use of ids for HTML elements, specifically paragraph tags, is very useful when running automation tests using tools such as selenium or in my current project that uses AngularJS, when running e2e tests using Protractor. It is much easier to make expectations by element ids than using class selectors.
我发现在使用诸如 selenium 之类的工具运行自动化测试或在我当前使用 AngularJS 的项目中使用 Protractor 运行 e2e 测试时,对 HTML 元素(特别是段落标记)使用 id 非常有用。通过元素 id 产生期望比使用类选择器容易得多。