我正在通过创建脚本标签来动态加载js,我已经在脚本加载中编写了回调函数,但是当js被加载后调用函数时, t访问我的对象属性,当我在萤火虫中检查它的值是缺失的。下面是我的代码,请回答我的代码中有什么问题,或者是否有其他方法来做同样的事情。在javascript回调函数中没有获取现有对象属性的值
obj = {
cont: null,
create:function(){
this.cont = document.createElement('iframe');
this.cont.style.setProperty('background-color', '#FFFFFF', 'important');
this.getJs();
},
getJs:function(){
var oHead = document.getElementsByTagName('head')[0];
var oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.src = 'myjs.js';
oScript.onload = obj.show;//most browsers
oScript.onreadystatechange = function() {// IE 6 & 7
if (this.readyState == 'complete') {
obj.show();
}
}
oHead.appendChild(oScript);
},
show:function(){
alert(this.cont);//this.cont is not available when js load and show function get called as a
} }obj.create();
您可以设置事件功能的范围。看到这个问题http://stackoverflow.com/questions/183214/javascript-callback-scope – PiTheNumber