Javascript commonjs中的addEventListener
我需要你的帮助。假设我有两个文件 文件1 在文件2中Javascript commonjs中的addEventListener,javascript,titanium,commonjs,Javascript,Titanium,Commonjs,我需要你的帮助。假设我有两个文件 文件1 在文件2中 var view = Ti.UI.createView(); var a = require('file1'); a = new test(); view.add(a.view); //no problem 现在我想将eventListeners添加到视图中 文件2 但是有没有办法在文件1中添加要查看的eventlisteners?像这样的 文件1 这样做会导致以下错误 Uncaught TypeError: Cannot s
var view = Ti.UI.createView();
var a = require('file1');
a = new test();
view.add(a.view);
//no problem
现在我想将eventListeners添加到视图中
文件2
但是有没有办法在文件1中添加要查看的eventlisteners?像这样的
文件1
这样做会导致以下错误
Uncaught TypeError: Cannot set property 'backgroundColor' of undefined
事件侦听器与视图和
test
函数相关。所以当你这样做的时候:
this.view.addEventListener('click',function(){
this.view.backgroundColor = 'red';
});
您正试图访问视图中的背景色
,此视图中的背景色
在附加事件之前捕获外部作用域,并在执行单击时使用它:
function test(){
var _this = this;
this.view = Ti.UI.createView({
backgroundColor : 'white'
});
this.view.addEventListener('click',function(){
_this.view.backgroundColor = 'red';
});
}
这将为您提供所需的正确引用。事件侦听器与视图和test
函数相关。所以当你这样做的时候:
this.view.addEventListener('click',function(){
this.view.backgroundColor = 'red';
});
您正试图访问视图中的背景色
,此视图中的背景色
在附加事件之前捕获外部作用域,并在执行单击时使用它:
function test(){
var _this = this;
this.view = Ti.UI.createView({
backgroundColor : 'white'
});
this.view.addEventListener('click',function(){
_this.view.backgroundColor = 'red';
});
}
这将为您提供所需的正确引用。事件侦听器与视图和test
函数相关。所以当你这样做的时候:
this.view.addEventListener('click',function(){
this.view.backgroundColor = 'red';
});
您正试图访问视图中的背景色
,此视图中的背景色
在附加事件之前捕获外部作用域,并在执行单击时使用它:
function test(){
var _this = this;
this.view = Ti.UI.createView({
backgroundColor : 'white'
});
this.view.addEventListener('click',function(){
_this.view.backgroundColor = 'red';
});
}
这将为您提供所需的正确引用。事件侦听器与视图和test
函数相关。所以当你这样做的时候:
this.view.addEventListener('click',function(){
this.view.backgroundColor = 'red';
});
您正试图访问视图中的背景色
,此视图中的背景色
在附加事件之前捕获外部作用域,并在执行单击时使用它:
function test(){
var _this = this;
this.view = Ti.UI.createView({
backgroundColor : 'white'
});
this.view.addEventListener('click',function(){
_this.view.backgroundColor = 'red';
});
}
这将为您提供正确的参考