用于较新的HTML5输入的jQuery选择器无法正常工作

时间:2013-01-29 15:45:37

标签: jquery

我有一个输出输入的数组,带有一个数字类型,但我似乎无法让选择器找到它。它不受支持吗?

$('#container :text').each(...
$('#container input:number').each(...
$('#container :number').each(...

他们都没有找到输入值。我错过了什么?

我还查看了jQuery站点上的选择器列表,并没有看到任何更新的HTML5输入类型。

1 个答案:

答案 0 :(得分:1)

jQuery 目前不支持某些伪选择器。相反,您需要将查询基于目标元素的属性,如下所示。

$("#container input[type=number]");

从jQuery 1.9.0开始,这些是存储在jQuery.expr[":"]下的伪选择器:

["animated", "button", "checkbox", "checked", "contains", "disabled", "empty", 
 "enabled", "eq", "even", "file", "first", "focus", "gt", "has", "header", 
 "hidden", "image", "input", "lang", "last", "lt", "not", "nth", "odd", 
 "parent", "password", "radio", "reset", "root", "selected", "submit", "target", 
 "text", "visible"]

如果您愿意,可以扩展此对象并提供自己的自定义选择器:

$.extend( $.expr[":"], {
    'color': function ( elem ) {
        return /color/.test(elem.type);
    }
});