它现在存在于 CSS4 中,请参阅 this answer。
否则,对于 jQuery,你可以使用...
$(':input[name]').filter(function() {
return this.value.toLowerCase() == 'search';
});
jsFiddle.
您还可以制作自定义选择器...
$.expr[':'].valueCaseInsensitive = function(node, stackIndex, properties){
return node.value.toLowerCase() == properties[3];
};
var searchInputs = $(':input:valueCaseInsensitive("Search")');
jsFiddle.
自定义选择器如果只做一次就有点过头了,但如果你需要在应用程序中多次使用它,这可能是个好主意。
更新
是否可以为任何属性使用这种自定义选择器?
当然,请查看以下示例。这有点复杂(:input[value:toLowerCase="search"] 之类的语法可能更直观),但它有效:)
$.expr[':'].attrCaseInsensitive = function(node, stackIndex, properties){
var args = properties[3].split(',').map(function(arg) {
return arg.replace(/^\s*["']|["']\s*$/g, '');
});
return $(node).attr(args[0]).toLowerCase() == args[1];
};
var searchInputs = $('input:attrCaseInsensitive(value, "search")');
jsFiddle.
您可能可以使用eval() 将该字符串设为数组,但我发现这样做更舒服(而且您不会意外执行放置在选择器中的任何代码)。
相反,我在, 分隔符上拆分字符串,然后在每个数组成员的任一侧剥离空格、' 和"。请注意,引号内的, 不会按字面意思对待。没有理由从字面上要求一个,但您总是可以针对这种可能性进行编码。我会把它留给你。 :)
我不认为map() 具有最好的浏览器支持,因此您可以显式迭代args 数组或augment the Array object。