2012-07-12 78 views
0

我有一個表名爲「recordsource」的屬性,該屬性將保存將填充表的內容的對象的名稱。設置一個屬性作爲對象的引用

<table id="tbl" recordsource="myobj"> 

現在,這裏是我的功能:

var myobj; 

function obj() 
{ 
    this.code = new Array(); 
    this.name = new Array(); 
} 

myobj = new obj(); 
myobj.code = ["a","b","c"]; 
myobj.name = ["apple","banana","carrot"]; 

function populate_table() 
{ 
    mytable = document.getElementById("tbl"); 
    mytableobj = mytable.getAttribute("recordsource"); //this will return a string 
    //my problem is how to reference the recordsource to the myobj object that have 
    //the a,b,c array 
} 
+0

你的意思'myobj.recordsource = mytable.getAttribute( '記錄源');'?或者你的意思是你希望myobj中的數據存儲在'mytable.setAttribute('recordsource',..'? – bokonic 2012-07-12 02:40:01

+0

同意@bokonic。你在這裏的目標是什麼? – 2012-07-12 02:45:50

+0

@bokonic既不是先生,但那是可能的嗎?我的意思是我只是將recordource放在table標籤上,然後setAttribute到我想參考的實際對象上 – 2012-07-12 03:12:41

回答

0

試試這個window[ mytableobj ]它將返回myobj

+0

訪問現場演示:[link](http://tinkerbin.com/O9Q40ydJ) – 2012-07-12 02:54:28

+0

這將只有當所有對象都是全局對象時(這可能不是一個好主意)。 – grc 2012-07-12 02:55:33

+0

但是他的javascript代碼第一行將'myobj'設置爲全局。 – 2012-07-12 03:06:01

0

的一種方法是使用一個對象,因爲所有你希望能夠訪問其他對象的列表。

... 

var obj_list = { 
    'myobj': myobj 
}; 

function populate_table() 
{ 
    mytable = document.getElementById("tbl"); 
    mytableobj = mytable.getAttribute("recordsource"); 

    // Then obj_list[mytableobj] == myobj 

    obj_list[mytableobj].code[0] // Gives "a" 
    obj_list[mytableobj].name[0] // Gives "apple" 
} 
相關問題