jQuery选择器:定位表单中的输入

jQuery selectors: targeting inputs in a form

本文关键字:输入 表单 定位 选择器 jQuery      更新时间:2023-09-26

我有以下内容:

$('#registration_form input[type=text], #registration_form input[type=password]')

我觉得它可以更好:更快,更短

之类的
$('#registration_form input[type=text,password]')

编辑:这个是我真正想要的!请看这里

您可以使用find()与:text和:password选择器来缩短语法:

$("#registration_form").find("input:text, input:password")

从性能的角度来看,它可能会稍微快一点,但不会快太多。

EDIT:由于:text:password不是本地CSS选择器,使用filter()的以下变化可能更快(并且首先它更短):

$("#registration_form input").filter(":text, :password")

你所拥有的还不错,如果你真的想要"更干净"的语法,你可以试试这个:

$("#registration_form").find("input[type=text], input[type=password]")

稍微干净一些,但不一定更快。它现在只需要查找一次#registration_form,而不是两次,但是您在那里添加了第二个方法调用。此外,由于您已经匹配了一个精确的id,因此在上面的示例中,您可能会节省很少的处理时间。

恐怕不存在这样的选择器。但是,如果你想简化你的选择器,你可以使用一个类。