0

编辑:我发现问题与在没有特定 API 或类似的东西的情况下无法访问 iframe 中的元素有关。已经解决了。

我需要嵌入每日天文图片网站的图像。这是我现在拥有的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 侧边栏扩展,因此在添加.innerHTML:时我不断收到此错误Uncaught type error: cannot read property 'innerHTML' of undifined

我通过操作在此答案中获得的代码获得了此代码,但每日天文图片图像不包含id.

4

1 回答 1

0

undefined 属性表示尚未为变量赋值。

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


我建议你调试一下你的代码。首先,这似乎没有得到任何东西iframe.contentWindow.document.getElementsByTagName("a")[1],因此您应该首先对其进行调试以查看它是否包含您要查找的元素。

因此,您应该使用控制台日志进行调试,看看会得到什么:

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

希望这可以帮助你。

于 2017-09-27T16:27:16.023 回答