0
我有一个循环,像这样:访问属性
for (var i = 0; i < 10; i++) {
obj[i] = new MapArea(some passed variables);
}
现在这个构造有一些预定义的属性,以及一些在初始化定义。正如for循环所暗示的,每个obj都保存在数组obj []中的自己的索引中。我的问题是,我已经通过初始化迭代后,我不能用
this.propertyName;
或
$(this).propertyName;
插件引用单个对象的属性,我的建筑工作过的鼠标事件(点击和悬停)这意味着我需要能够检查附加到事件上的obj是否具有当前状态的特定属性,但是没有办法通过编程知道它在数组中的哪个索引来引用它,或者至少可以轻松简洁地实现。
有没有人遇到过这个问题,并找到一个解决方案,或者我几乎被迫使用数组和索引作为参考?任何帮助都是极好的。
这里是我的方法,例如一个:
$.fn.clickLight = function(options) {
var defaults = $.extend({
color : "#43464B",
opacity : "0.3"
}, options);
ctx.globalAlpha = defaults.opacity;
$(area_ref).click(function(event) {
$(this).handleMouse(event).each(function() {
if (!$(this).clicked) { // I try and access here
console.log(obj.this.clicked);
$(this).highlight(defaults.color);
$(this).clicked = true;
} else {
console.log(this.clicked);
$(this).clearlight();
$(this).clicked = false;
}
});
});
$(area_ref).hover(function() {
$(this).handleMouse().each(function() {
$(this).highlight(defaults.color);
});
},function() {
if (!$(this).clicked){ // I try and access here
$(this).handleMouse().each(function() {
$(this).clearLight();
});
}
});
return $(this);
};
你必须建立某种联系。您可以将索引保存在[数据属性](https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Using_data_attributes)或手动绑定每个对象的点击处理程序(不建议) –
提供一些代码,你使用'this.propertyName' – Maxx
ahhh,我喜欢数据属性的想法。我完全忘记了这一点。我现在就试试 – Turk