Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript commonjs中的addEventListener_Javascript_Titanium_Commonjs - Fatal编程技术网

Javascript commonjs中的addEventListener

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

我需要你的帮助。假设我有两个文件

文件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 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';
     });
 }
这将为您提供正确的参考