gpt4 book ai didi

javascript - 无法访问 iframe 中的 canvas 元素

转载 作者:行者123 更新时间:2023-11-28 21:05:27 29 4
gpt4 key购买 nike

我有一个 iframe,我想在此 iframe 中选择一个 Canvas 元素。但这不起作用。我正在使用以下代码。

这是我的 iframe

<body>
<div id="canvasdiv" width="300" height="300"></div>
</body>

这是我的父页面

<iframe name="iframe" id="iframe" frameborder="0" width="325" height="325" src="..."></iframe>

使用 javascript,我添加了带有一些属性的 Canvas 。例如 ID。

canvas.node.id = "canvas";

在我的父页面中,我得到了以下 JavaScript。

var iframe = document.getElementById("iframe");
alert(iframe);
var iframe_canvas = iframe.contentDocument || iframe.contentWindow.document;
alert(iframe_canvas);
var canvas = iframe_canvas.getElementById("canvas");
alert(canvas);

结果:

iframe = iframe element
iframe_canvas = html element
canvas = null

有人知道为什么我无法访问 Canvas 元素吗? =(

最佳答案

抱歉,我已经解决了这个问题。感谢所有的答复。 =)

为了测试 getElementById 函数,我通过 $(document).ready 调用了它。但此时浏览器还没有渲染 iframe。这就是为什么我可以访问 iframe 本身,但不能访问 iframe 的元素。现在我将该功能分配给一个按钮并且它可以工作。

关于javascript - 无法访问 iframe 中的 canvas 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9996595/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com