现在替换.selector属性,它在jQuery 1.9中被删除 [英] Alternative to .selector property now that it is removed in jQuery 1.9

查看:185
本文介绍了现在替换.selector属性,它在jQuery 1.9中被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从jQuery 1.9开始,jQuery对象的 .selector 属性已被删除。 (我有点困惑,为什么,确切)。我实际上在一些独特的场景中使用它,我知道我可以做其他事情来防止这种情况。 只是想知道是否有人知道从1.9开始抓住选择器的其他方式?

As of jQuery 1.9 the .selector property of jQuery objects has been removed. (I'm a little confused as to why, exactly). I actually use it in a few unique scenarios, and I know that I could do other things to prevent this. Just wondering if anyone knows another way of grabbing the selector as of 1.9?

$('#whatever').selector // (would of returned '#whatever')  

我在哪里的一个例子需要 .selector 是指我已经有一组名称的复选框,我希望在该组中看到哪一个已选中

One example of where I need .selector is when I already have a group of checkboxes by name, and I want to see, within that group, which one is checked:

jsFiddle DEMO

jsFiddle DEMO

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属性

jQuery $上已弃用的.selector属性的剩余用途b $ b对象是支持已弃用的.live()事件。在1.9中,jQuery没有
更长时间尝试在链式方法中维护这个属性,因为
使用链式方法从不支持.live()。不要
在jQuery对象上使用.selector属性。 jQuery Migrate
插件不会尝试维护此属性。

The remaining purpose of the deprecated .selector property on a jQuery object is to support the deprecated .live() event. In 1.9, jQuery no longer attempts to maintain this property in chained methods, since the use of chained methods was never supported with .live(). Do not use the .selector property on a jQuery object. The jQuery Migrate plugin does not attempt to maintain this property.


推荐答案

那里实际上需要原始选择器的原因应该不多。在您的特定用例中,如果要缩小所选元素的范围,可以使用 .filter [docs]

There should not be many reasons to actually need the original selector. In your specific use case, if you want to narrow down the set of selected elements, you can use .filter [docs]:

$test.filter(':checked').attr('id')



< hr>

$('#whatever')。selector 似乎仍然有效。文档说在1.9中,jQuery不再尝试在链式方法 [...]中维护此属性。虽然 http://api.jquery.com/selector 声称它已在1.9中删除。我不知道,这有点令人困惑。


$('#whatever').selector still seems to work though. The documentation says "In 1.9, jQuery no longer attempts to maintain this property in chained methods [...]". Though http://api.jquery.com/selector claims it was removed in 1.9. I don't know, it's a bit confusing.

这篇关于现在替换.selector属性,它在jQuery 1.9中被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆