jQuery中类和id有什么区别? [英] What's the difference between class and id in jQuery?

查看:134
本文介绍了jQuery中类和id有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery中的class和id有什么区别?例如:

What is the difference between class and id in jQuery? For example:

<span class="lalal"></span>

<span id="lalal"></span>

因为其中一个适用于jQuery,另一个不是真的。
Thanks。

Because one works good with jQuery and another not really. Thanks.

推荐答案

ID在页面上应该是唯一的,当您有多个具有相同ID的元素时,jQuery选择只有第一个。那是因为它不必费心寻找别人,因为不应该有更多的东西 - ndash;这可能会解释您遇到的奇怪行为。

ID's should be unique on the page, when you have multiple elements with same ID's, jQuery selects only the first one. That's because it doesn't have to bother looking for others as there isn't supposed to be any more – that might explain the weird behaviour you're experiencing.

如果您希望多个元素具有相同的功能,请为他们提供相同的类别。如果你想识别一个特定的元素,给它一个id。这不仅限于jQuery,还包括HTML和CSS。

If you want multiple elements to have the same functionality, give them the same class. If you want to identify a specific element, give it an id. This isn't limited to just jQuery, but to HTML and CSS overall.

这篇关于jQuery中类和id有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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