[英]Append to div if no class or ID
我正在使用JavaScript在頁面內創建iFrame,但我不知道父級的詳細信息,例如類名或ID或其他任何內容。
是否可以在不知道此信息的情況下創建iFrame?
我嘗試了以下操作,但收到錯誤HierarchyRequestError: Node cannot be inserted at the specified point in the hierarchy
:
var iframe = document.createElement('iframe');
iframe.src = 'http://myURL.com/embed/whatever/1213';
window.parent.document.appendChild(iframe);
更新為澄清起見,我問是否可以在div,頁面內而不是正文末尾以這種方式創建iframe。
我知道我能做
<iframe src="whatever"></iframe>
但我寧願使用JavaScript解決方案
您需要將附加代碼更改為:
window.parent.document.body.appendChild(iframe);
因為iframe
在內部也只是另一個文檔,所以您不能將文檔附加到文檔中。
如果要將其附加到ID為“ iframeDiv”的div,則可以執行以下操作:
document.getElementById("iframeDiv").appendChild(iframe);
但是您可以將其附加到<body>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.