window.parent.document在firefox中工作,但不在chrome和IE中

Nis*_*nce 8 html javascript iframe jquery

我的概念是从iframe更新主页面中文本框的值.此代码工作中firefox,但没有工作Internet ExplorerChrome.双方main.htmlframe.html都在同一个位置.我需要建议让它在所有浏览器中运行.

main.html中

<!DOCTYPE html>
<html>
<head>
<title> main window </title>
</head>
<body>
Parent textbox :<input type="text" id="parentbox"></br></br></br>
<iframe src="frame.html" ></iframe>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

frame.html

<!DOCTYPE html>
<html>
<head>
<title> frame window </title>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
function PushValues()
{
 var frame_value = document.getElementById("framebox").value;
 window.parent.document.getElementById("parentbox").value =frame_value;
}
</script>
</head>
<body>
<input type="text" id="framebox" >
<input type="button" value ="fill" onclick="PushValues()">
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Pra*_*een 0

既然你正在使用 jQuery 试试这个

var frame_value = $('#framebox').val();
$('#parentbox', window.parent.document).val(frame_value);
Run Code Online (Sandbox Code Playgroud)

  • @user2564024那么你应该看一下http://softwareas.com/cross-domain-communication-with-iframes (2认同)