jQuery 1.9.1属性选择器 [英] jQuery 1.9.1 property selector
问题描述
从jQuery 1.9开始, .attr(..)
方法不再返回属性值,我们现在必须使用 .prop(..)
。不幸的是,这也适用于通过属性选择器指定的属性,即 $(input [value =])
p
请参阅
http://jquery.com/upgrade-guide /1.9/#attr-versus-prop-
以及关于 .attr $ c之间差异的良好SO讨论$ c>和
.prop
:
我目前使用的选择器如 $(input [value =])
和 $( select [value =])
选择没有设置值的输入元素。然而,这不再适用于jQuery 1.9,而是我现在正在做这样的事情:
$ p $
var hasValue = function() {return !!($(this).val()。length); };
var hasNoValue = function(){return!($(this).val()。length); };
$(input)。filter(hasValue);
$(select)。filter(hasValue);
我的实际选择器有点大,检查有或没有值的多个元素,现在我有将我的1选择器字符串拆分为多个选择器,其间使用.filter(..)方法调用。
问题 是否有等价于 谢谢 如果这确实是您应该受到的谴责,您可以创建自定义选择器。 http://jsfiddle.net/ExplosionPIlls/zaZPp/ 编辑:您也可以使用 Background As of jQuery 1.9 the See
http://jquery.com/upgrade-guide/1.9/#attr-versus-prop- and a good SO discussion on the differences between My Situation I'm currently using selectors like My actual selectors are a little larger, checking multiple elements with or without values so now I'm having to split my 1 selector string into multiple selectors with .filter(..) method calls in between. Question Is there an equivalent to Thanks Using If this really is that reprehensible to you, you could create a custom selector. http://jsfiddle.net/ExplosionPIlls/zaZPp/ EDIT: You may also be able to get away with using 这篇关于jQuery 1.9.1属性选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ ([value =])
, $([value!=])
, $(值='abc'])
它使用属性而不是属性?如果没有,是否有比使用 .filter(hasValue)
和 .filter(hasNoValue)
方法更简洁的方法?
。过滤器
似乎是唯一的方法,但它不是太糟糕,你可以通过使用 .val
: p>
$(:input)。filter(function(){return $(this).val()=== ;});
$。expr [':']。emptyInput = function(elem){
return $(elem).is(:input )&& $(elem).val()===;
};
this.value
而不是 $(elem).val()
。.attr(..)
method no longer returns property values, instead we now have to use .prop(..)
. Unfortunately this also applies to attributes specified via an attributes selector i.e. $("input[value=]")
.attr
and .prop
:$("input[value=]")
and $("select[value=]")
to select input elements that have no value set. However, this no longer works with jQuery 1.9, instead I'm now doing something like this:var hasValue = function () { return !!($(this).val().length); };
var hasNoValue = function () { return !($(this).val().length); };
$("input").filter(hasValue);
$("select").filter(hasValue);
$("[value=]")
, $("[value!=]")
, $("[value='abc']")
which uses the property instead of the attribute? And if not, is there a cleaner way than using the .filter(hasValue)
and .filter(hasNoValue)
methods?.filter
seems to be the only way, but it's not too bad and you can actually make it a little more accurate by using .val
:$(":input").filter(function () { return $(this).val() === ""; });
$.expr[':'].emptyInput = function (elem) {
return $(elem).is(":input") && $(elem).val() === "";
};
this.value
instead of $(elem).val()
.