var U = function(){
this.div = document.createElement('DIV');
this.div.id= 'oooofffff';
this.div.innerHTML = '<form action=""><input type="file" name="trainingReport" /><input type="submit" value="Upload" id="Upload" /></form>';
document.body.appendChild(this.div);
$(this.div).css({'text-align':'center','padding-top':'25px'});
$('input#Upload',this.div).button();
$(this.div).dialog({
title:'Upload Summer Training Report',
resizable:false,
position:['center',300],
show:'blind',
hide:'explode',
autoOpen:false
});
}
U.uploadReport = function(ApplicationID){
console.log(this.div); //outputs undefined
$(this.div).dialog("open");
}
$(document).ready(U);
我的對象有什麼問題?當我打電話給U.uploadReport()
功能時,它看不到this.div
對象。我應該怎麼做才能解決它?面向對象的javascript錯誤
注:請不要提供,我可以使用$('#oooofffff').dialog('open')
編輯: 版本2:
var U = function(){
var that = this;
that.div = document.createElement('DIV');
that.div.id= 'oooofffff';
that.div.innerHTML = '<form action=""><input type="file" name="trainingReport" /><input type="submit" value="Upload" id="Upload" /></form>';
$(document).ready(function(){U.initialize()});
that.uploadReport = function(ApplicationID){
console.log(that.div);
$(that.div).dialog("open");
}
that.initialize = function(){
document.body.appendChild(that.div);
$(that.div).css({'text-align':'center','padding-top':'25px'});
$('input#Upload',that.div).button();
$(that.div).dialog({
title:'Upload Summer Training Report',
resizable:false,
position:['center',300],
show:'blind',
hide:'explode',
autoOpen:false
});
}
return that;
}();
你在某種程度上試圖使用一個普通的函數作爲實例,它不工作....''這裏面'U'指'文檔'和'U .uploadReport'到'window'對象。我建議閱讀:https://developer.mozilla.org/en/JavaScript/Guide/Working_with_Objects –
如果我使它'var U = new function(){...'它工作嗎? //編輯:我試過了,但它不起作用;) –
您只能將一個函數傳遞給'$(document).ready',而不是一個對象。 –