As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center作为指导。
7年前关闭。
在jquery中使用名称和ID有什么优势
但是,您可以为多个可能无法平等找到元素的项目赋予相同的名称。
7年前关闭。
<input type ="button" name="buttonsel" id="buttonse1"/>
$("#buttonse1")
VS $("[name=buttonse1]")
在jquery中使用名称和ID有什么优势
最佳答案
编辑
name =中的名称在表单中必须唯一。 id =中的名称在整个文档中必须是唯一的。
JavaScript需要唯一的名称,但是这里已经有太多没有唯一的name =名称的文档,因此W3人们发明了唯一的id标签。不幸的是,较旧的浏览器无法理解。因此,您需要表单中的两种命名方案。
在这里查看完整的详细信息:http://mindprod.com/jgloss/htmlforms.html#IDVSNAME
id可以识别元素的唯一性。
有效的html包含每个元素的唯一ID。
<input type ="button" name="buttonsel" id="buttonse1"/>
<input type ="button" name="buttonsel" id="newbutton"/>//valid for both id and name
<input type ="button" name="buttonsel" id="buttonse1"/>//not valid as id repeated but name is valid even if repeated
但是,您可以为多个可能无法平等找到元素的项目赋予相同的名称。
关于jquery - 在jquery中使用name vs id有什么优点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13191356/