main.htm<html> 
<body> 
<table cellpadding="10" border='2'><tr><td>用户名:</td><td id='User'>Admin</td></tr></table><p></p>
<h1 id="myH2">主页面-容器</h1><p></p>
<Iframe src="plugin.htm" width="250" height="200" scrolling="no" frameborder="5"></iframe> 
</body> 
</html>plugin.htm
<html> 
<body onload="CurrentUser.innerText='当前用户:'+parent.User.innerText"> 
<h1>子框架-插件</h1> 
<p id="CurrentUser"></p>
</body> 
</html>需要给一个现有的Web程序添加功能,通过编写plugin页面来定制一些功能,插件要取得主页面中的当前用户信息,上面的例子是可以取得的,但是现在主页面的内容是:
<html> 
<body> 
<table cellpadding="10" border='2'><tr><td>用户名:</td><td>Admin</td></tr></table><p></p>
<h1 id="myH2">主页面-容器</h1><p></p>
<Iframe src="plugin.htm" width="250" height="200" scrolling="no" frameborder="5"></iframe> 
</body> 
</html>
即用户名单元格没有ID,因为原程序页面是WebSphere生成无法更改,插件页要如何写才能把主页面中的当前用户提取出来,我想方法大概应该是枚举出第一个表格,然后再取第二个单元格的值,因为是Web开发的新手,试验好一阵搞不定,只有来这里请高人帮忙了,谢谢!

解决方案 »

  1.   


    <html>  
    <script>
    window.onload= function(){
       var tb = parent.document.getElementsByTagName("table")[0];
       document.getElementById("CurrentUser").innerHTML = tb.rows[0].cells[1].innerHTML;
    }
    </script>
    <body>
    <h1>子框架-插件</h1>  
    <p id="CurrentUser"></p>
    </body>  
    </html>
      

  2.   


    <html>  
    <body onload="CurrentUser.innerText='当前用户:' + parent.document.getElementsByTagName('table')[0].firstChild.firstChild.childNodes[1].innerText">  
    <h1>子框架-插件</h1>  
    <p id="CurrentUser"></p>
    </body> 
    </html>
      

  3.   

    对啊,table直接rows[0].cells[1]就行了。