1. 程式人生 > >frameset裡的一個frame1使用js獲取另一個frame2裡的內容

frameset裡的一個frame1使用js獲取另一個frame2裡的內容

 index.html 頁面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>index</title>
    </head>
    <frameset  border='0' frameborder="no" frameSpacing='0' cols="20%,80%">
      <frame name="frame1"  id="frame1" src="frame1.html"  scrolling='auto' border='0' />
      <frame name="frame2" id="frame2" src="frame2.html"  scrolling='auto' border='0' />
    </frameset>

</html>

frame1.html  頁面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>frame1</title>
    </head>
    <body>
        <script type="text/javascript">
            function methodF2(userName){
                //獲取父元素(index.html)裡的frame2(frame2.html)的物件
                var frame2=window.parent.document.getElementById("frame2").contentWindow.document;
                //設定frame2.html頁面上id為userName的<input/>標籤的值
                moduleDocument.getElementById("userName").value=userName;
                //提交frame2.html頁面的form表單
                moduleDocument.frm.submit();
            }
        </script>
        <button onclick="methodF2('張三')">button</button>
    </body>

</html>

frame2.html 頁面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>frame2</title>
    </head>
    <body>
         <form action="url" method="post" name="frm" id="frm">
            <input type="text" value="" id="userName" name="userName"/>
            <input type="submit" value="submit"/>
         </form>
    </body>
</html>