自从 jQuery 1.9 版本开始,jQuery 对象的 .selector 属性已被移除。(我对其确切原因有些困惑)。我在一些独特的情况下实际上会使用它,并且我知道我可以做其他事情来避免这种情况。请问是否有人知道在 1.9 版本之后另一种获取选择器的方法?
从文档中得知,jQuery对象上的已弃用的 .selector 属性的剩余目的是支持已弃用的 .live() 事件。自从 1.9 版本开始,jQuery不再尝试在链式方法中维护此属性,因为使用 .live() 永远不支持链式方法。请勿在 jQuery 对象上使用 .selector 属性。jQuery Migrate 插件也不会尝试维护此属性。
$('#whatever').selector // (would of returned '#whatever')
我需要使用 .selector 的一个例子是当我已经有一组通过name进行分组的复选框,并且我想查看该组中哪个被 checked:
var $test = $('input[name="test"]');
console.log( $test );
console.log( $(':checked', $test).attr('id') ); // returns --undefined--
console.log( 'now with .selector: ');
console.log( $($test.selector + ':checked').attr('id') ); // returns correct
从文档中得知,jQuery对象上的已弃用的 .selector 属性的剩余目的是支持已弃用的 .live() 事件。自从 1.9 版本开始,jQuery不再尝试在链式方法中维护此属性,因为使用 .live() 永远不支持链式方法。请勿在 jQuery 对象上使用 .selector 属性。jQuery Migrate 插件也不会尝试维护此属性。
$('#whatever').selector
似乎仍然有效。文档说:“在1.9中,jQuery不再尝试在链接方法中维护此属性[...]”。但是http://api.jquery.com/selector/声称已经删除了它。我不知道,有点混淆。我想官方声明可能会澄清这一点,也许您可以在他们的邮件列表/论坛/组/任何地方发布。 - Felix Kling.filter
:http://api.jquery.com/filter/。`$test.filter(':checked').attr('id')`。$(':checked', $test)
无法工作,因为input
元素没有任何后代。 - Felix Kling