2010-09-10 59 views
0

Default.aspxjavascript函数有助于通过框架的src中的ID获取元素?

<html> 
    <frameset id="MainFrameset" rows="78,*" border="0" framespacing="0" frameborder="0"> 
     <frame application="yes" name="menuFrame" src="<% = GetMenuFrameSrcUrl() %>" scrolling="no" noresize> 
    </frameset> 
</html> 

Default.aspx.cs

public string GetMenuFrameSrcUrl() 
{ 
    return "http://localhost/Application1/Pages/MenuPage.aspx" 
} 

MenuPage.aspx

<%@ Register TagPrefix="customControl" TagName="Menu" Src="/Controls/Menu.ascx" %> 
<html> 
    <body bottommargin="0" leftmargin="0" topmargin="0" rightmargin="0"> 
     <form id="Form1" method="post" runat="server"> 
      <customControl:menu id="Menu1" runat="server"> 
      </customControl:menu> 
     </form> 
    </body> 
</html> 

AnotherPage.aspx

<head id="Head1" runat="server"> 
    <script type="text/javascript"> 
     function testFunction(args, name) { 

      //Need help here...How can I get the reference of Menu1 id? 

      //I can get this: 
      alert(top.frames[0].name);  //menuFrame 
      //But don't know how I can take it further to get some Element ID from the MenuPage.aspx which is the source of this Frame??? 
     } 
    </script> 
</head> 

问题:如何从testFunction获取Menu1 id的引用?

感谢,

巫毒

回答

1

框架文件诀窍是这样的:

var frm = top.frames[0]; 
var doc = frm.contentDocument || frm.Document; // Document (capitalized) in IE, contentDocument otherwise 
var menu = doc.getElementById("Menu1"); 

但是,如果菜单1有不同的客户端ID,你可以试试这个路线:

MenuPage.aspx 

<script type="text/javascript"> 
function getMenu() { 
    return document.getElementByID("<%=Menu1.ClientID%>"); 
} 
</script> 

AnotherPage.aspx 

var menu = top.frames[0].getMenu(); 
0

您可以使用ASP.NET服务器代码片断做到这一点。请注意,这假定您的javascipt函数包含在Default.aspx的标记中(与帧相同的页面)。

<head id="Head1" runat="server"> 
    <script type="text/javascript"> 
     function testFunction(args, name) { 

      var menu1 = document.getElementById('<%=Menu1.ClientID %>'); 
      //note I've never done this with framesets, so it might be 
      top.frames[0].document.getElementById('<%=Menu1.ClientID %>'); 
     } 
    </script> 
</head> 
+0

假设该功能在默认.aspx是不正确的。该函数必须位于另一个页面中,特别是'AnotherPage.aspx' – VoodooChild 2010-09-10 02:36:07