【发布时间】:2016-01-26 04:59:54
【问题描述】:
我想从包含在 WooCommerce (http://www.shapur.com/product-category/fp-journe/) 中的 prettyPhoto 灯箱插件中的 iframe 中删除不必要的标题。当您单击该页面底部的单个产品按钮时,它应该删除 iframe 中包含的 body 标记。 iframe 符合同源策略,所以不是这样。
我在同一域上创建了一个测试页面,效果很好:http://www.shapur.com/test.php。
与灯箱有冲突吗?我只是不明白。感谢您的帮助。
这是测试页面的代码:
<!DOCTYPE html>
<html>
<body>
<iframe id="myframe" src="http://shapur.com/index.php"></iframe>
<p>Click the button to change the style of the body tag contained in the iframe.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction() {
var x = document.getElementById("myframe");
var y = x.contentDocument;
y.body.style.display = "none";
}
</script>
</body>
</html>
【问题讨论】:
-
iframe 内容在具有 iframe 的页面的 DOM 中不可见,因此您无法更改它。要删除它,您需要在托管的页面上触发事件通过 iframe。
-
需要确保 iframe 先加载
-
@DinoMyte 在测试页面 (shapur.com/test.php) 我能够在 iframe 中托管的页面上触发事件。
标签: javascript jquery wordpress iframe same-origin-policy