javascriptはiframeのページの元素値を取得する方法です。

2813 ワード

IE方法:document.frames.'myFrame'.document.getElement ById('test').value;
火狐の方法:document.getElemenntById.『myFrame』.contentWindow.document.getElement ById..value;
IE、フォックス方法:

    function getValue(){


         var tmp = '';


         if(document.frames){


                tmp += 'ie :';


                tmp += document.frames['myFrame'].document.getElementById('test').value;


         }else{


                tmp = document.getElementById('myFrame').contentWindow.document.getElementById('test').value;


         }


         alert(tmp);


    }

例コード:a.ページのコード

<html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                <title>
                    javascript iframe
                </title>
           </head>
           <body>
                <iframe id="myFrame" src='b.html' style="width:300px;height: 50px;"></iframe>
                <input type="button" id="btn" onclick="getValue()" value="test" >
                <script type="text/javascript">
                        function getValue(){
                            var tmp = '';
                            if(document.frames){
                                    tmp += 'ie :';
                                    tmp += document.frames['myFrame'].document.getElementById('test').value;
                            }else{
                                    tmp = document.getElementById('myFrame').contentWindow.document.getElementById('test').value;
                            }
                            alert(tmp);
                        }
                </script>
            </body>
        </html>
b.htmlページのコード

       <html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                <title>
                    iframe
                </title>
            </head>
            <body>
                <input type='text' id="test" value=' :justflyhigh.com'>
            </body>
        </html>