类或ID [英] class or ID

查看:143
本文介绍了类或ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


重复:

Duplicate:

  • Cascading style sheets use "id" or "class"
  • CSS: are class and id interchangeable?
  • div class vs id
  • CSS: div id VS. div class

有时,当我喜欢做选择时, (unique)to something(images)and i used jquery to do domething when hover or clic。

Sometime, when i like to make a selection a add an ID (unique) to something (images) and i used jquery to do domething when hover or clic.

但是同样的事情可以用类来完成。我知道类是很多东西(让我们说class =selected),但是当一个类添加到某个东西我可以触发一个jQuery动作与该类的inst ...

But the same thing can be done with classes. I understand that class is for a lot of thing (let say class="selected") but when a class is add to something i can trigger a jQuery action with that class insted of ID...

所以问题是,如果我可以完成任何事情和一切,使用类和ID是什么是正确的方式。

So the question is, what is "the right way" to use class and ID if i can accomplish anything and everything with both !

推荐答案

在哪里使用ID和类



两者之间的简单区别是,一个类可以在页面上重复使用,每个页面只能使用一次ID。因此,在div元素上使用标识页面上主要内容的ID是恰当的,因为只有一个主要内容部分。相反,您必须使用类在表格上设置交替的行颜色,因为按照定义,它们将被多次使用。

Where to use an ID versus a class

The simple difference between the two is that while a class can be used repeatedly on a page, an ID must only be used once per page. Therefore, it is appropriate to use an ID on the div element that is marking up the main content on the page, as there will only be one main content section. In contrast, you must use a class to set up alternating row colors on a table, as they are by definition going to be used more than once.

ID是令人难以置信的强大的工具。具有ID的元素可以是以某种方式操纵元素或其内容的JavaScript的目标。 ID属性可以用作内部链接的目标,用名称属性替换锚点标记。最后,如果你使你的ID清晰和逻辑,他们可以作为一种自我文档在文档中。例如,如果块的开始标签的ID为main,header,footer,则不需要在块之前添加注释,说明代码块将包含主要内容等。

IDs are an incredibly powerful tool. An element with an ID can be the target of a piece of JavaScript that manipulates the element or its contents in some way. The ID attribute can be used as the target of an internal link, replacing anchor tags with name attributes. Finally, if you make your IDs clear and logical, they can serve as a sort of "self documentation" within the document. For example, you do not necessarily need to add a comment before a block stating that a block of code will contain the main content if the opening tag of the block has an ID of, say, "main", "header", "footer", etc.

这篇关于类或ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆