2012-05-15 85 views
2

我需要插入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插入源代码(使用最后一段代码片段)。

任何想法?

回答

8

1 - 您可以使用element.appendChild

makediv.appendChild(makeIframe); 

2或追加的iframe作为HTML到像这个div,

makediv.innerHTML = '<iframe src="http://aol.com" style="border: 0pt none ;'+ 
        'left: -453px; top: -70px; position: absolute;'+ 
        'width: 1440px;'+ 
        'height: 775px;" scrolling="no"></iframe>'; 

不是插入makediv你想去的地方,

var getRef = document.getElementById("div-id-to-insert-element-before"); 
var parentDiv = getRef.parentNode; 
parentDiv.insertBefore(makediv, getRef); 

更新:

,不能设置风格setAttribute功能,

var makeIframe = document.createElement("iframe"); 
makeIframe.setAttribute("src", "http://aol.com"); 
makeIframe.setAttribute("scrolling", "no"); 
makeIframe.style.border = "none"; 
makeIframe.style.left = "-453px"; 
makeIframe.style.top = "-70px"; 
makeIframe.style.position = "absolute"; 
makeIframe.style.width = "1440px"; 
makeIframe.style.height = "775px"; 

var makediv = document.createElement("div"); 
makediv.style.height = "43px"; 
makediv.style.width = "564px"; 
makediv.style.position = "relative"; 
makediv.style.overflow = "hidden"; 
+0

虽然这两方面的建议,实际上是获得了iframe入格,出于某种原因,iframe中显示了全尺寸(它“爆”出来的DIV)。 – Learning

+0

换句话说,div不包含iframe并隐藏溢出,因为它应该。虽然HTML代码的工作原理与此相同,但它会显示整个完整页面的iframe。 iframed页面的左上角显示与div插入页面的左上角对齐,这告诉我iframe在div内。当你加载用纯HTML编写的相同代码时,div并不会阻止iframe显示完整大小的方式。也许这是一个油门瑕疵?或者JS代码有问题吗? – Learning

+0

@Robert,我第一次看不到,但是你的代码还有一个错误,你不能用'setAttribute'函数设置元素样式,再次查看我更新的答案。 – ocanal