如何通过标记名称在Opera扩展中嵌入网站的一部分

问题描述:

编辑: 我发现问题与无法访问iframe中的元素而没有特定的API或类似的东西有关。它已经解决了。如何通过标记名称在Opera扩展中嵌入网站的一部分

我需要嵌入Astronomy Picture of the Day网站的图像。这里是我现在的html:

<head> 
<style> 
    body, html { 
     margin: 0px; 
     padding: 0px; 
    } 
    iframe { 
     border: none; 
    } 
</style> 
</head> 
<body> 
    <iframe id="iframe" width="100%" height="600px" 
src="https://apod.nasa.gov/apod/astropix.html"></iframe> 
    <div id="PlaceToPutTable"></div> 
    <script src="panel.js"></script> 
</body> 

这里是我现在的JavaScript:

var iframe = document.getElementById("iframe"); 
var div = document.getElementById("PlaceToPutTable"); 
div.innerHTML = 
iframe.contentWindow.document.getElementsByTagName("a")[1]; 

我一直在使用iframe.contentWindow.document.getElementByTagName("img")[0]与不.innerHTML下它试图。 我将此作为Opera Sidebar Extension使用,因此在添加.innerHTML时出现此错误:Uncaught type error: cannot read property 'innerHTML' of undifined

我通过操纵我在this answer处得到的代码得到了这段代码,但是天天图片的图片不包含id

The undefined property indicates that a variable has not been assigned a value.

你没有从那里得到任何东西。


我建议你调试一下你的代码。首先,它似乎没有得到任何东西iframe.contentWindow.document.getElementsByTagName("a")[1],所以你应该先调试它,看看它是否包含你正在寻找的元素。

所以,你应该有一个控制台日志调试,看看这得到:

console.log(iframe.contentWindow.document.getElementsByTagName("img")); 

希望这有助于你出去。

+0

当我这样做时,控制台出现此错误:Uncaught TypeError:iframe.contentWindow.document.getElementByTagName不是函数。 –

+0

看起来像一个错字。它的getElementsByTagName(注意元素上的复数)。现在编辑抱歉 –