2012-07-12 80 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" 
} 
相关问题