ネイティブJSを使用して、親ページでiframeチャイルドページの要素を取得し、子ページで親ページの要素を取得します。これは、よく使用される方法です。これが要約する例です。
1。親ページ(demo.html) 、親ページの子ページdivの背景色を灰色に変更すると、それは赤であることが判明します。
< xmlns = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <タイトル>デモメインページ</</<= "= javascript"> javascript "> javascript"> javascript "> javascript =" document.getElementById( 'iframeid')。contentWindow; var _div = _iframe.document.getelementbyid( 'objid'); _ div.style.backgroundcolor = '#ccc';} </script> </head> <body> <div '> div> div> <> < src = "demo-iframe.html" id = "iframeid"> </iframe> </body> </html>
2。Subpage(demo-iframe.html) 、子ページの親ページdivのフォント色を赤に変更すると、黒であることが判明しました。
< xmlns = "http://www.w3.org/1999/xhtml"> <head> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <title>サブページdemo13-iframe.html </javas = < function(){var _iframe = window.parent; var _div = _iframe.document.getelementbyid( 'pardiv'); _ div.style.color = 'red';} </scrip> </head> <body> <div id = 'objid' style = 'width:100px; height:100px; background-color:red;'> subpage </div> </body> </html>3。複製図:
(1)JSが追加されない場合のレンダリング:
(2)JSを追加した後のレンダリング:
上記の記事「ネイティブJS」では、IFRAMEのDOM要素を取得します - 父と息子のページは、互いのDOM要素を取得します。私はそれがあなたに参照を与えることができることを願っています、そして私は誰もがwulin.comをもっとサポートできることを願っています。