2011-11-07 29 views
0

我用JavaScript很新的,我不明白這個問題的:無法訪問函數出事件

$(function() { 
    var $tab_title_input = $("#tab_title"), 
    $tab_content_input = $("#tab_content"); 
    var tab_counter = 0; 
    var editors = {}; 
    var tab_current = 0; 

    // tabs init with a custom tab template and an "add" callback filling in the content 
    var $tabs = $("#tabs").tabs({ 
     tabTemplate: "<li><a href='#{href}'>#{label}</a> <span class='ui-icon ui-icon-close'>Remove Tab</span></li>", 
     add: function(event, ui) { 
      var tab_content = $tab_content_input.val() || "Tab " + tab_counter + " content."; 
      $(ui.panel).append("<div id=\"editor" + tab_counter + "\" class=\"editor\">" + tab_content + "</div>"); 
      adjust_size(); 
      tab_current = ui.index; 
      editors[tab_current] = ace.edit("editor" + tab_counter); 
      }, 
     show: function(event, ui) { 
      adjust_size(); 
      tab_current = ui.index; // zero-based index 
      editors[tab_current].resize(); 
     }, 
     select: function(event, ui) { 
      adjust_size(); 
      tab_current = ui.index; // zero-based index 
      }, 

    }); 

的問題是,這行代碼:

editors[tab_current].resize(); 

打破了一切,告訴Uncaught TypeError: Cannot call method 'resize' of undefined

但編輯editors[tab_current].resize()在添加事件被明確界定,並alert(tab_current)給了我正確的結果。

回答

1

我打賭的錢,editors[tab_current]回報undefined

您的alert(tab_current)可能會返回正確的值,但這並不意味着存在與其對應的editors元素。用alert(editors[tab_current])進行測試,如果顯示undefined,則檢查元件是否設置正確。

+0

是它如此或相似:第一次說不確定,但隨後的翻譯:它似乎正確設置(它的工作原理) –

+0

顯然它沒有得到正確設置你第一次去叫它。在運行時跟蹤代碼,找出原因。 – Polynomial

+0

我該怎麼做? Chrome的控制檯不提供任何信息 –

0

我可以看到調查兩種途徑直線距離:

  1. 什麼ace.edit("editor" + tab_counter)回報?它是否總是使用resize方法返回對象,或者它有時會返回undefined?

  2. add對於tab_current的任何值總是被調用show之前?

+0

1),則返回[對象對象],它總是有一個調整大小的方法,但爲了安全,我試圖與他人所有的工作在添加事件,但不是在母豬或選擇 –

+0

2-)我想是這樣,但在創建第一個標籤時,它說未定義的其他對象的對象 –