gpt4 book ai didi

javascript - 为 iFrame 文档添加打印按钮

转载 作者:行者123 更新时间:2023-12-02 22:23:47 25 4
gpt4 key购买 nike

所以我有一个 iFrame,它链接到另一个网站上的外包报价。是否可以为该 iFrame 内的结果添加打印按钮?

我尝试了以下代码,但它根本没有做太多事情 - 单击按钮时,它什么也不做

<iframe src="https://www.compulife.net/website/1295/quoter.html" width="750 pixels" id="test" name="test" height="750 pixels" frameborder="0" formtarget="_blank"></iframe>

<input type="button" value="Print" onclick="test()" />

<script>
function test {
window.frames["test"].focus();
window.frames["test"].print();
}
</script>

错误:

Uncaught TypeError: test is not a function
at HTMLInputElement.onclick

我可能做错了什么吗?

enter image description here

最佳答案

我认为您的函数有两个错误,而不是

test() 你刚刚调用了 test ,其次

Uncaught TypeError: test is not a function at HTMLInputElement.onclick

来自window.frames["test"].print();,您可以使用它代替

document.getElementById("test").contentWindow.print();

即使您在提到后成功清除了此错误我有一个链接到另一个网站上的外包报价的 iFrame 也不会得到结果,因为

你最终会遇到这样的错误

SecurityError: Blocked a frame with origin from accessing a cross-origin frame

这是因为您无法使用 JavaScript 访问具有不同来源的 iframe,如果您可以这样做,这将是一个巨大的安全缺陷。

请参阅解决方法:SecurityError: Blocked a frame with origin from accessing a cross-origin frame

关于javascript - 为 iFrame 文档添加打印按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59132307/

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