【发布时间】:2017-12-06 10:36:25
【问题描述】:
当 .gridlove-sidebar-action 在 iframe 中时,我想在 in parent 中运行脚本。 Iframe 不在同一个域中,但 AccessControlAllowOrigin 在 htaccess、css 和脚本中被“激活”,除此之外。如果 iframe 位于同一域中,则该脚本正在运行。我拥有这两个域。
内嵌框架:
<iframe id="followblog" src="http://example.com/iframe.html"></iframe>
在 iframe 中:
<span class="gridlove-sidebar-action">
<a class="#" href="#">
My button
</a>
</span>
在父级中运行的脚本:
$('#followblog').load(function(){
var iframe = $('#followblog').contents();
iframe.find(".gridlove-sidebar-action").click(function(){
$('body').addClass('gridlove-sidebar-action-open gridlove-lock');
$('.gridlove-sidebar-action-wrapper').css('top', gridlove_admin_top_bar_height);
});
});
htaccess:
SetEnvIf Origin "http(s)?://(www\.)?(example.com)$" AccessControlAllowOrigin=$0$1
Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
Header set Access-Control-Allow-Credentials true
【问题讨论】:
-
你需要使用
jQueryiframecontents()概念:- stackoverflow.com/a/13782102/4248328 -
我编辑了问题
标签: javascript jquery html iframe