Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这是jquery的有效HTML吗_Jquery_Html_Jquery Selectors - Fatal编程技术网

这是jquery的有效HTML吗

这是jquery的有效HTML吗,jquery,html,jquery-selectors,Jquery,Html,Jquery Selectors,我有下面的HTML div#main-content div#right-col div#post-show div#post-img div#post-img-large{ } div#main-content div#right-col div#post-show div#post-img div#post-img-large img#post-img-large{ } 如您所见,我对div和img使用相同的id 允许这样做吗?我知道它工作得很好,但是如果我参考img large

我有下面的HTML

div#main-content div#right-col div#post-show div#post-img div#post-img-large{

}
div#main-content div#right-col div#post-show div#post-img div#post-img-large 
   img#post-img-large{
}
如您所见,我对div和img使用相同的id


允许这样做吗?我知道它工作得很好,但是如果我参考img large后的id,jQuery会感到困惑吗?

根据以前的经验:

对于id选择器,jQuery使用JavaScript函数 document.getElementById,非常高效。当另一个 选择器附加到id选择器,例如h2pageTitle、jQuery 在将图元标识为 匹配

每个id值在文档中只能使用一次。如果超过 一个元素被分配了相同的ID,即使用该ID的查询 将仅选择DOM中第一个匹配的元素。这种行为 然而,不应依赖于;包含多个文档的文档 使用相同ID的元素无效


来源:

根据以前的经验:

对于id选择器,jQuery使用JavaScript函数 document.getElementById,非常高效。当另一个 选择器附加到id选择器,例如h2pageTitle、jQuery 在将图元标识为 匹配

每个id值在文档中只能使用一次。如果超过 一个元素被分配了相同的ID,即使用该ID的查询 将仅选择DOM中第一个匹配的元素。这种行为 然而,不应依赖于;包含多个文档的文档 使用相同ID的元素无效


来源:

ID不应用于任何给定页面上的多个元素。它们用于唯一标识元素

现在,在您的特定示例中,如果您正在使用jQuery,那么总是可以通过使用更多信息限定选择器来选择所需的元素。divID、imgID-jQuery不会混淆,但是您应该遵守标准,而不是重用ID

来自W3c: html4,但它仍然适用

7.5.2 Element identifiers: the id and class attributes

Attribute definitions

id = name [CS]
This attribute assigns a name to an element. This name must be unique in a document.
class = cdata-list [CS]
This attribute assigns a class name or set of class names to an element. Any number of elements may be assigned the same class name or names. Multiple class names must be separated by white space characters.

ID不应用于任何给定页面上的多个元素。它们用于唯一标识元素

现在,在您的特定示例中,如果您正在使用jQuery,那么总是可以通过使用更多信息限定选择器来选择所需的元素。divID、imgID-jQuery不会混淆,但是您应该遵守标准,而不是重用ID

来自W3c: html4,但它仍然适用

7.5.2 Element identifiers: the id and class attributes

Attribute definitions

id = name [CS]
This attribute assigns a name to an element. This name must be unique in a document.
class = cdata-list [CS]
This attribute assigns a class name or set of class names to an element. Any number of elements may be assigned the same class name or names. Multiple class names must be separated by white space characters.
这不是有效的html,ID必须是唯一的

是的,由于重复的ID,jquery可能无法按预期工作。事实上,就在几天前,我在这里看到了一个功能中断的问题。

这不是有效的html,ID必须是唯一的

是的,由于重复的ID,jquery可能无法按预期工作。事实上,就在几天前,我在这里看到了一个功能中断的问题。

检查中的每个代码。

检查中的每个代码