重复的ID值会破坏jQuery选择器吗? [英] Do duplicate ID values screw up jQuery selectors?
问题描述
如果我有两个div,两个id均为"myDiv", $(#myDiv").fadeOut();
会使两个div消失吗?还是只会在第一/第二秒消失?还是根本没有?
If I had two divs, both with id="myDiv", would $("#myDiv").fadeOut();
fade both divs out? Or would it fade only the first/second? Or none at all?
如何更改它淡出的那一个?
How do I change which one it fades out?
注意::我知道重复的ID违反标准,但是我使用的是fancybox模态弹出式窗口,它会为页面上的内容复制页面上的指定内容.如果有人知道解决此问题的方法(也许我使用的是fancybox错误),请告诉我.
Note: I know duplicate id's is against standards but I'm using the fancybox modal popup and it duplicates specified content on your page for the content of the popup. If anyone knows a way around this (maybe I'm using fancybox wrong) please let me know.
推荐答案
jQuery在查询ID时正好匹配一个元素. $(#foo").get()
将返回最多一个 Element
对象的数组.有关更多信息,请参见 jQuery文档,或者自己尝试.
jQuery matches exactly one element when querying for an ID. An array of at most one Element
object will be returned by $("#foo").get()
. See the jQuery documentation for more information, or try it yourself.
$(function() {
alert($("#foo").length);
});
这篇关于重复的ID值会破坏jQuery选择器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!