Titanium Javascript:“;那"不起作用

Titanium Javascript: "that." does not work

本文关键字:quot 不起作用 Titanium Javascript      更新时间:2023-09-26

这个和那个都不起作用。有人知道发生了什么事吗??

编辑:在其他代码中,qwerty被简单地称为"qwerty();"。它被认为是独立的。

编辑:我意识到哪里不对。问题出在我…

function qwerty () {
..... for loop that changes i ......
var that = this;
this.chara[i] = createlabel.....
this.chara[i].addEventListener('click', function(e) {
    var j = e.source.id;
    alert("hello word");
    alert(this.chara[j].width); // I get the error here
});
this.chara[i].addEventListener('doubleclick', function(e) {
    alert("hello word");
    alert(that.chara[i].width); // I get the error here too.
});
}

任何与this相关的JS问题都可能是由于调用使用this的函数的方式造成的。在that变量中存储对this的引用应该可以让您从嵌套函数中引用它,这与您已经执行的方式完全相同——假设qwerty()的调用方式首先将this设置为正确的对象。(就我个人而言,我喜欢将这样的变量称为self,因为它更准确地反映了变量的作用。)

然而,在你的函数中,你说你得到了这行的错误:

that.chara[i].width

假设你说this.chara[i].addEventListener(...),我猜chara[i]变量包含对DOM元素的引用。如果是这种情况,我猜它是一个没有width属性的元素类型。试试这个:

that.chara[i].style.width

https://developer.mozilla.org/en/CSS/width

这是我能为你做的最好的事情,而不需要更多关于你得到了什么错误以及如何调用qwerty()函数的信息。。。