在jquery对象的集合上使用underscore.js列表函数
use underscore.js list functions on collection of jquery objects
我正在使用一个同时使用jQuery和undercore.js的应用程序。我希望能够在jQuery对象的集合上使用下划线的一些迭代器函数,例如any()
和all()
。有办法做到这一点吗?我想做一些类似于以下的事情:
checkboxes = $("input[type=checkbox]");
_.filter(checkboxes, function(box) {
return box.is(":checked");
});
但这引发了一个错误:
Uncaught TypeError: Object #<HTMLInputElement> has no method 'is'
所以我假设这个上下文中的box并不像jQuery对象那样。
您必须将box
包装在jQuery:中
checkboxes = $("input[type=checkbox]");
checkboxes = _.filter(checkboxes, function(box) {
return $(box).is(":checked");
});
此外,您可以只使用本机box.checked
:,而不是为集合中的每个元素创建一个新对象
checkboxes = $("input[type=checkbox]");
checkboxes = _.filter(checkboxes, function(box) {
return box.checked;
});
附带说明:jQuery有自己的过滤方法:
checkboxes = $("input[type=checkbox]").filter(function() {
return $(this).is(":checked");
});
此外,在你的例子中,你确定你必须过滤吗?你可以像你的选择器一样轻松地使用它:
checkboxes = $("input[type=checkbox]:checked")
这里box
是一个HTMLInputElement
类型的对象。它不是jQuery对象。由于.is
是一个jQuery对象方法,因此需要box
中的jQuery对象。
它可以通过$(box)
来完成。然后应用.is()
。
$(box).is(":checked");
相关文章:
- Underscore.js某些对象的所有方法的总和
- JavaScript(Underscore.js)扩展功能
- 在underscore.js中显示列表中的所有项目(使用Parse.com)
- 使用Underscore.js修改json数组中所选元素的更有效方法
- CLNDR.js将变量传递到underscore.js中
- 如何使用带前导0的underscore.js生成一系列数字
- underscore.js在_之外没有数据.each
- context参数在underscore.js中做什么
- lodash/underscore.js函数来创建由x的n个副本组成的数组
- 使用underscore.js在深度嵌套的JSON中查找值
- 如果键与underscore.js匹配,请比较两个数组
- underscore.js IIFE语法有效吗
- 如何将Underscore.js过滤器与对象一起使用
- Underscore.js:具有_.object函数的意外行为
- Underscore.js _.isElement function
- Does underscore.js在_中创建匿名func对象,每个对象都需要进行乱码处理
- underscore.js,名为“”的全局对象;出口;和livefyre javascript API-集成冲突
- 使用underscore.js比较两个Object
- Underscore.js and noconflict
- underscore.js,迭代器引用错误