簡體   English   中英

如何使用 onload 事件定位框架集的框架

[英]how to target a frame of a frameset with an onload event

我有一個這樣的框架集:

<frameset rows="120px,100%">
<frame name="top" id="top" src="top.php" noresize frameborder="0" scrolling="no" />
<frame name="main" id="main" src="main.php" noresize frameborder="0" scrolling="yes" />

在 main.php 文件中,我有:

<script>
function reload_top() {
    top.frames['top'].location.href= "http://www.google.com";
}
</script>

而 main.php 主體的 onload 事件是:

<body onload="reload_top();">

我想要實現的是,每次有人加載 main.php 時,該文件都會從頂部重新加載頂部框架 top.php,但到目前為止我只能讓它脫離框架集並加載一個新頁面...

我已經嘗試過以下任何一種:

document.getElementById('top').src = "http://www.google.com";

window.top.location.href = "http://www.google.com";

top.frames['top'].location.href= "http://www.google.com";

他們都沒有工作。 我究竟做錯了什么?

謝謝

在這里嘗試另一個答案,我們在框架集元素中使用 onload 事件

<html>
 <head>
 <title>Frame: Onload & Onunload Examples</title>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 <script language="JavaScript">
 <!--
  function testOnload() {
   // alert('OnLoad: Alert in testOnload() executed by onLoad property of frameset tag.');
  document.getElementById("top").src="http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_win_frames2";
  }
 function testOnUnload() {
 //alert('OnUnload: Alert in testOnUnload() executed by onUnLoad property of frameset tag.')
  }
  //-->
  </script>
  </head>
   <frameset rows="80,*" frameborder="0" border="0" framespacing="0" 
   onLoad="javascript:testOnload()"   
   onUnload="javascript:testOnUnload(); alert('onUnload: alert in frameset tag')">
<frame name="topFrame" scrolling="NO" noresize src="https://developers.google.com/oauthplayground/" id="top">
<frame name="mainFrame" src="https://developers.google.com/oauthplayground/">
<noframes>
    <body bgcolor="#FFFFFF">
     <p>Sorry, this page needs a browser that supports frames.</p>
    </body>
</noframes>
</frameset>
</html>

作為信息,HTML5 不再支持frames 標簽。 您想使用 iframe 在一頁中加載不同的頁面嗎?

您無法設置框架位置的問題是因為您嘗試訪問框架時未定義

例如代碼

<iframe id="topf" src="stacktop.php"></iframe>
<script>
     function reload_top() {
        var a = document.getElementById('topf');
         a.src = "http://www.aapastech.com";
    }
 </script>

Html5 不支持框架集,但使用 jquery 並嘗試它

 <!DOCTYPE html>
 <html>
 <head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js">        </script>
 <script>
 $(document).ready(function(){

        document.getElementById("top").src="http://yahoo.com";
 });
</script>
</head>
<frameset cols="25%,*,25%" id="abc">
 <frame src="http://www.google.com" id="top" name="top">
 <frame src="http://www.google.com">
 <frame src="http://www.google.com">
  </frameset>
 </html>

經過更多的挖掘,我發現從另一個框架定位一個框架的方法是使用:

parent.document.getElementById('frameName').src

所以我的函數現在可以工作了,看起來像這樣:

<script>
    function reload_top() {
        parent.document.getElementById('top').src = "venues.php";
    }
</script>

感謝您嘗試幫助家伙。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM