有没有一种简单的方法来选择所有类型的文本框?
即。 <input type=
文本或电子邮件或网址或号码 />
我不介意密码或文本区域。
例如。我有这段代码,它选择具有自定义属性(“特殊”)的所有文本输入,可以缩短吗?$("input[type='text'][special],input[type='email'][special],input[type='url'][special],input[type='password'][special]")
最佳答案
:input selector 是吗?涵盖您感兴趣的案例?
$(":input[special]")
文档:
The
:input selector
basically selects all form controls.Additional Notes:
Because:input
is a jQuery extension and not part of the CSS specification, queries using:input
cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using:input
to select elements, first select the elements using a pure CSS selector, then use.filter(":input")
.
$("[special]").filter(":input");
关于用于选择所有类型文本框的 jQuery 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13149248/