gpt4 book ai didi

javascript - 无法访问我的 iframe 中的跨域父元素

转载 作者:行者123 更新时间:2023-11-30 18:57:46 27 4
gpt4 key购买 nike

下面是我的问题的描述:

我已将小书签功能添加到我的站点。为此,我在我的站点中提供了一个名为“我的书签”的链接。因此,我将该链接拖到书签并将其添加到书签。所以,从现在开始,当我访问其他网站时说“yahoo.com”.. 我喜欢那个网站的任何内容,所以我可以选择该内容并单击书签中的“我的书签”,它会打开我的一页站点作为“yahoo.com”的子元素。

为此,我在 javascript 中添加了以下代码。其中 ifrme src 是我网站的 url。

 var div = document.createElement("div");

div.id = "instacalc_bookmarklet";

var str = "";
str += "<table id='instacalc_bookmarklet_table' valign='top' width='570' cellspacing='0' cellpadding='0'><tr><td width ='850' height='880'>";
str += "<iframe frameborder='2' scrolling='yes' name='instacalc_bookmarklet_iframe' id='instacalc_bookmarklet_iframe' src='" + iframe_url + "' width='850px' height='875px' style='textalign:right; backgroundColor: white;'></iframe>";
str += "</td><td id='closetd' onClick='toggleItem(\"instacalc_bookmarklet\");' style='background: #FFDDDD;' title='click to close window' valign='top' align='center' width='20px'>";
str += "<a href='javascript:void(0);' style='width:100%; text-align: middle; color: #FF0000; font-family: Arial;'>x</a>";
str += "</td></tr></table>";

div.innerHTML = str;

div.onkeypress = keyPressHandler;
document.body.insertBefore(div, document.body.firstChild);

你可以在这里标记,我添加了一个表格标签,里面有两个 td.. 一个 td 在 iframe 中显示我的页面,另一个 td 用于“关闭”按钮.. 这将从“yahoo”关闭这个打开的窗口.com”。

现在,我的问题是.. 我的站点中有按钮“关闭”.. 单击它... 我想关闭窗口.. (与上面 javascript 中的“关闭”按钮相同的操作..调用“toggleItem”函数)

但我的问题是..在我的 iframe 中我无法访问 div 元素..

我写的是这样的

<a href="#bookmarklet" onclick="closethis();">Close</a>


<script>

function closethis()
{

id='instacalc_bookmarklet';
var item = parent.document.getElementById(id);
if(item){
if ( item.style.display == "none"){
item.style.display = "";
}
else{
item.style.display = "none";
}
}
}


</script>

但是单击该关闭链接.. 我在 var item = parent.document.getElementById(id); 行上收到 javascript 错误“获取属性 window.document 的权限被拒绝”;

有什么想法吗?

提前致谢。

最佳答案

出于安全原因,javascript 禁止这样做,您无法更改它。它们是绕过限制的一些方法:

  • 使用回调函数并将数据加载为 JSONP。这可能不适合您的情况。
  • 在网页中创建一个表单,其中包含一个针对前域的操作,提交它,获得响应,然后删除该表单。

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

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