尝试在选择器的迷你对象中使用 .find() 和 .children 时遇到困难
having difficulty trying to use .find() and .children inside a mini object of selectors
我基本上试图摆脱过程式js方法,因此尝试更多地使用对象。我已经在我的整个对象中设置了一个对象来缓存选择器并设置变量等,但我似乎遇到了问题。目前,当我运行此代码时,我收到 2 个错误,告诉我 .length 和 .children 都是未定义的。有人可以解释设置这些的正确方法,以便可以使用jquery方法吗?
.JS
var bop = {
els: {
// cache selectors ...
grid: $( '#grid' ),
box: this.grid.children(),
box_l: this.box.length,
start_btn: $( '#start' ),
counter: $( '#counter' ),
sequence: 5
}
}
如果你期望this
引用你正在使用对象文字构建的正在构建的对象,那么这就是你的问题。JavaScript不是这样工作的。
您可以这样做:
var bop = {
els: {
grid: $('#grid'),
// ...
}
};
bop.els.box = box.els.grid.children();
// etc
this
引用完全是关于函数调用的,实际上与对象无关,或者不像你来自 C++/C#/Java 背景时想象的那么多。
在文本对象中使用 this
时,它不引用正在创建的对象,它仍然与文本对象外部的代码相同。
如果您
非常想使用this
,请将缓存对象更改为
els: {
// cache selectors ...
grid: $( '#grid' ),
box: function(){
return this.grid.children()
},
box_l: function(){
return this.box().length
},
start_btn: $( '#start' ),
counter: $( '#counter' ),
sequence: 5
}
其他明智的做法是确保在使用box
和box_1
属性时引用$( '#grid' )
。
附带说明一下,这个小提琴将帮助您理解为什么您的this
不能按照您认为的方式工作。干杯。
相关文章:
- .Find()仅显示可见元素
- jQuery(this).Pparent().Pparente().Prent().find()在iPad上不起作用
- jquery UI draggable:UI.children不是一个函数
- 他们是如何使用angular/jqLite find()方法按属性名称和值选择元素的?ng conf 2015
- jQuery find() Issue
- 使用jquery Find()在复选框中查找图像
- strongloop script.js run find in before delete方法:如何同步运行异步方法
- React.cloneElement:传递新的孩子或复制props.children
- 在另一个Find(..)的回调内部查找,如何逃离回调地狱
- $(e.target).find和template.find('input').value之间有什么区别
- 为什么我得到了一个“;未定义的“;使用“”从Find()返回值;这个“;论点
- .map-find元素一旦通过AJAX加载
- MongoDB collection.find()查询挂起
- 如何通过使用jQuery单击该DIV来获取children DIV类名
- 如何在jQuery中使用find
- 无法将参数传递给mongo find集合
- Ember.js-模型find()方法中的异步调用
- mongodb中db.collection.find()的返回类型是什么
- 使用 jquery .find() 遍历按类和存储属性选择元素
- 尝试在选择器的迷你对象中使用 .find() 和 .children 时遇到困难