回答編集履歴

2

コメントに対応してスクリプトを修正(追加)しました

2020/12/21 03:50

投稿

ockeghem
ockeghem

スコア11701

test CHANGED
@@ -141,3 +141,89 @@
141
141
  </body>
142
142
 
143
143
  ```
144
+
145
+
146
+
147
+ ---
148
+
149
+ (追記 2020/12/21 12:40)
150
+
151
+ コメント見ました。
152
+
153
+ iframeの内側のボタンのみを複製するスクリプトを書いてみました。JavaScriptを外側に移すとなるとオオゴトだと思います(プロパティも変更しないといけない)ので、それはやっていません。
154
+
155
+
156
+
157
+ iframeのガワ(外)
158
+
159
+ ```HTML
160
+
161
+ <body>
162
+
163
+ <div id="b"></div>
164
+
165
+ <script>
166
+
167
+ function importButton(bName) { // ボタンを複製して配置。bName はボタンのID
168
+
169
+ const oldNode = document.querySelector("iframe").contentWindow.document.getElementById(bName);
170
+
171
+ const newNode = document.importNode(oldNode, true); // 複製ノードを作る
172
+
173
+ newNode.onclick = oldNode.onclick; // onclickイベントをコピーする
174
+
175
+ oldNode.style.display ="none"; // iframe内の元のボタンは非表示にする
176
+
177
+ pNode = document.getElementById("b").parentNode;
178
+
179
+ pNode.insertBefore(newNode, pNode.firstElementChild); // 複製したノードをインサートする
180
+
181
+ }
182
+
183
+ window.onload=function(){
184
+
185
+ importButton('btn_b');
186
+
187
+ importButton('btn_a');
188
+
189
+ }
190
+
191
+ </script>
192
+
193
+ <iframe src="311122c.html" id="iframe"></iframe>
194
+
195
+ </body>
196
+
197
+ ```
198
+
199
+
200
+
201
+ iframeの内(child.html)
202
+
203
+ ```HTML
204
+
205
+ <body>
206
+
207
+ <script>
208
+
209
+ function a(){
210
+
211
+ alert("フレーム内のalert(a)");
212
+
213
+ }
214
+
215
+ function b(){
216
+
217
+ alert("フレーム内のalert(b)");
218
+
219
+ }
220
+
221
+ </script>
222
+
223
+ <input id="btn_a" type="button" value="ボタン(btn-a)" onclick="a()">
224
+
225
+ <input id="btn_b" type="button" value="ボタン(btn-b)" onclick="b()">
226
+
227
+ </body>
228
+
229
+ ```

1

コメントの内容を受けて追記しました

2020/12/21 03:50

投稿

ockeghem
ockeghem

スコア11701

test CHANGED
@@ -67,3 +67,77 @@
67
67
  </body>
68
68
 
69
69
  ```
70
+
71
+
72
+
73
+ ---
74
+
75
+ (追記 2020/12/21)
76
+
77
+
78
+
79
+ 以下ではどうでしょうか? 関数とボタンの両方をiframeの外に持ち出しました。内側のボタンとJavaScriptは、なくても動きます。
80
+
81
+
82
+
83
+ iframeのガワ(外)
84
+
85
+ ```HTML
86
+
87
+ <body>
88
+
89
+ <script>
90
+
91
+ function b1() {
92
+
93
+ // 関数 c1 をiframeの外側に持ち出した
94
+
95
+ // iframeの document取り出し
96
+
97
+ var childDoc = document.getElementById('iframe').contentDocument;
98
+
99
+ // iframe内のDOM操作
100
+
101
+ childDoc.getElementById('c').textContent = 'b1';
102
+
103
+ }
104
+
105
+ </script>
106
+
107
+ <input type="button" value="ボタン1" onclick="b1()"><br> <!-- 外側のボタン -->
108
+
109
+ <iframe src="child.html" id="iframe"></iframe>
110
+
111
+ </body>
112
+
113
+ ```
114
+
115
+
116
+
117
+ iframeの内(child.html)
118
+
119
+ ```HTML
120
+
121
+ <body>
122
+
123
+ <input type="button" value="ボタン1" onclick="c1()"/><br> <!-- 内側のボタン -->
124
+
125
+ <script>
126
+
127
+ function c1() {
128
+
129
+ // iframe内のDOM操作
130
+
131
+ document.getElementById('c').textContent = 'c1';
132
+
133
+ }
134
+
135
+ </script>
136
+
137
+ <div id="c"></div>
138
+
139
+ </script>
140
+
141
+ </body>
142
+
143
+ ```