addEventListener in commonjs
addEventListener in commonjs
我需要你的帮助。假设我有 2 个文件。
文件1
function test(){
this.view= Ti.UI.createView({
backgroundColor : 'white'
});
}
module.exports = test;
并在文件 2 中
var view = Ti.UI.createView();
var a = require('file1');
a = new test();
view.add(a.view);
//no problem
现在我想将事件侦听器添加到视图中。
文件2
var view = Ti.UI.createView();
var a = require('file1');
a=new test();
view.add(a.view);
a.view.addEventListener('click',function(){
a.view.backgroundColor = 'red';
});
//no problem with this too
但是有没有办法添加事件侦听器以在文件 1 中查看? 像这样的东西
文件1
function test(){
this.view = Ti.UI.createView({
backgroundColor : 'white'
});
this.view.addEventListener('click',function(){
this.view.backgroundColor = 'red';
});
}
这样做会给我以下错误
Uncaught TypeError: Cannot set property 'backgroundColor' of undefined
事件侦听器与视图和test
函数相关。因此,当您这样做时:
this.view.addEventListener('click',function(){
this.view.backgroundColor = 'red';
});
您正在尝试访问this.view
内view
内的backgroundColor
。
在追加事件之前捕获外部范围,并在执行单击时使用它:
function test(){
var _this = this;
this.view = Ti.UI.createView({
backgroundColor : 'white'
});
this.view.addEventListener('click',function(){
_this.view.backgroundColor = 'red';
});
}
这应该为您提供所需的正确参考。
相关文章:
- Node.js's Buffer.writeFloatBE in Javascript
- Setting default onclick behavior for <img> tag in gene
- Lucene Search in Alfresco
- grep in JQuery to C#
- 如何捆绑commonjs模块,除非指定需要
- write HTML in JavaScript
- jQuery setTimeOut: in for-loop
- SetTimeout and clearTimeout in Javascript
- lightbox in html 5 and javascript
- HTMLInputElement in IE7
- Mongodb$in以与数组中相同的顺序获取结果
- element.dataset in Internet Explorer
- jQuery parent() in table
- Instagram oauth flow in angularjs
- In循环的In运算符前后
- 如何使用for in循环在javascript中显示对象中的对象
- Jquery change onchange in onload
- 重构 jQuery in JS 问题
- strongloop script.js run find in before delete方法:如何同步运行异步方法
- addEventListener in commonjs