javascript – HTML5中的多个相同元素ID – 这应该如何与脚本一起使用?

前端之家收集整理的这篇文章主要介绍了javascript – HTML5中的多个相同元素ID – 这应该如何与脚本一起使用?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经回答了数百个jQuery问题.常见的新手错误之一是使用多个元素ID,如下所示:
<div id="a">....</div>
<div id="a">....</div>

然后他们会做这样的事情,并问为什么它不能按预期工作:

$('#a').hide();

我经常回复

06002

…有人总是回应:

But not in HTML5!

问题:因此,如果HTML5中允许多个相同的ID,那么脚本应该如何处理它们,或者我们是否应该避免使用多个相同的元素ID?

解决方法

仅引用 HTML5

The id attribute specifies its element’s unique identifier (ID).
[DOMCORE]

The value must be unique amongst all the IDs in the element’s home subtree
and must contain at least one character. The value must not
contain any space characters.

我想在一般意义上,ID可能是用词不当.但是,它应该在子树上下文中有效.

家庭子树是:

A node’s 07001 is the subtree rooted at that node’s root element. When a node is in a Document,its home subtree is that Document’s tree

原文链接:https://www.f2er.com/js/158984.html

猜你在找的JavaScript相关文章