2011-11-07 31 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-)我想是这样,但在创建第一个标签时,它说未定义的其他对象的对象 –