2017-08-15 63 views
-1

每當我嘗試訪問鉻開發人員工具中的對象,我看到錯誤波紋管: VM4939:1未捕獲TypeError:無法讀取未定義 的屬性「單元格」:1 :13在鉻開發工具中的對象訪問不起作用

我的代碼是:

<head> 
    <script> 
    var new_2ELayout; 
    function doOnLoad() { 
     var new_1CLayout = new dhtmlXLayoutObject({ 
     parent: document.body, 
     pattern: "1C" 
     }); 
     var new_1CLayoutA = new_1CLayout.cells("a"); 
    </script> 
</head> 
+2

該代碼在語法上不正確;你在某處丟失了一個'}'。 – Pointy

+0

看起來你只是錯過了你的功能的大括號。 –

回答

0

你在我看來,有2種方式來做到這一點:

1 - 申報外功能 var new_2ELayout, new_1CLayout;

2變量 - 聲明全局變量

但最好的所有選項都是第一個。

+0

不是我嫉妒,但爲什麼這被選爲接受的答案,而不是我的?我說同樣的事情,我提供了有效更正的有效代碼示例。 –

3

這是因爲new_1CLayoutdoOnLoad()函數中定義的,所以I'T不能從外部訪問。在外面宣佈。

var new_2ELayout, new_1CLayout; 

function doOnLoad() { 
     new_1CLayout = new dhtmlXLayoutObject({ 
      parent: document.body, 
      pattern: "1C" 
     }); 

     var new_1CLayoutA = new_1CLayout.cells("a"); 

} // and don't forget to close the function here 

// Now you can log new_1CLayout outside the function