我正试图在点击事件中从父窗口调用子窗口的功能。它不适用于Safari和Chrome,但它正在使用Firefox。我使用此代码从父窗口按钮事件
调用子窗口的功能var iframeElem = document.getElementById("iframe");
iframeElem.contentWindow.muteVideoSound();
静音窗口是在iframe中加载的子窗口的功能。
答案 0 :(得分:0)
您是否尝试从父窗口调用iframe中的函数?
可以这样做:
<强> main_window.html 强>
<!DOCTYPE html>
<html>
<head>
<title>main window</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script>
$(document).ready(function() {
$("a#update_text_link").click(function(event) {
event.preventDefault();
$("iframe#iframe_id")[0].contentWindow.update_text(); // run the function
});
});
</script>
</head>
<body>
<p><a href="#" id="update_text_link">Update iframe text</a></p>
<iframe src="iframe.html" id="iframe_id"></iframe>
</body>
</html>
<强> Iframe.html的强>
<!DOCTYPE html>
<html>
<head>
<title>iframe</title>
<script type="text/javascript" src="jquery-1.7.2.min.js"></script>
<script>
function update_text() {
$("h1").fadeOut(function () {
$(this).text("Some updated text.").fadeIn();
});
}
</script>
</head>
<body>
<h1>Some text.</h1>
</body>
</html>
但是,如果从本地文件系统加载网页,则无法在某些浏览器(如Chrome)中使用。因此,您需要将此示例放在Web服务器上,然后尝试在此类浏览器中打开它。
此外,如果您需要有关此主题的更多信息(包括对旧浏览器的支持),您可以在此处阅读类似问题的一些答案: