我需要插入iframe到使用javascript的div。我需要用JavaScript編寫,所以我可以在一個greasemonkey腳本中使用代碼(並且greasemonkey只允許javascript)。使用Javascript插入Iframe到Div - Greasemonkey
greasemonkey腳本將插入美國在線的搜索欄到各個網站。我已經在下面包含了可用的HTML代碼,所以您可以對它進行測試以查看最終結果是什麼時候生效。
這正是我需要做的,寫在HTML:
<div style="overflow: hidden; width: 564px; height: 43px; position: relative;">
<iframe src="http://aol.com" style="border: 0pt none ; left: -453px; top: -70px; position: absolute; width: 1440px; height: 775px;" scrolling="no"></iframe>
</div>
我需要在Greasemonkey的是HTML代碼的工作,這就要求它被寫在JavaScript。以下是我這麼遠(與「僞代碼」下面我的最後一個問題):
var makeIframe = document.createElement("iframe");
makeIframe.setAttribute("src", "http://aol.com");
makeIframe.setAttribute("scrolling", "no");
makeIframe.setAttribute("border", "0pt");
makeIframe.setAttribute("border", "none");
makeIframe.setAttribute("left", "-453px");
makeIframe.setAttribute("top", "-70px");
makeIframe.setAttribute("position", "absolute");
makeIframe.setAttribute("width", "1440px");
makeIframe.setAttribute("height", "775px");
makeIframe.setAttribute("scrolling", "no");
var makediv = document.createElement("div");
makediv.setAttribute("height", "43px");
makediv.setAttribute("width", "564px");
makediv.setAttribute("position", "relative");
makediv.setAttribute("overflow", "hidden");
我已經知道如何可以插入iframe或插入DIV到網站的源代碼我需要將其插入到,這樣做:
var getRef = document.getElementById("div-id-to-insert-element-before");
var parentDiv = getRef.parentNode;
parentDiv.insertBefore(iframe OR div, getRef);
我想不出該怎麼辦,是怎麼寫的iframe INTO股利,然後插入該div到源代碼。最後一段代碼片段適用於將div或iframe插入源代碼,但我需要div中的iframe,然後將該div插入源代碼(使用最後一段代碼片段)。
任何想法?
雖然這兩方面的建議,實際上是獲得了iframe入格,出於某種原因,iframe中顯示了全尺寸(它「爆」出來的DIV)。 – Learning
換句話說,div不包含iframe並隱藏溢出,因爲它應該。雖然HTML代碼的工作原理與此相同,但它會顯示整個完整頁面的iframe。 iframed頁面的左上角顯示與div插入頁面的左上角對齊,這告訴我iframe在div內。當你加載用純HTML編寫的相同代碼時,div並不會阻止iframe顯示完整大小的方式。也許這是一個油門瑕疵?或者JS代碼有問題嗎? – Learning
@Robert,我第一次看不到,但是你的代碼還有一個錯誤,你不能用'setAttribute'函數設置元素樣式,再次查看我更新的答案。 – ocanal