2012-11-27 20 views
0

我有一個示例代碼: index.html中如何使用javascript添加iframe中的值?

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns:fb="http://ogp.me/ns/fb#"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>index</title> 
</head> 
<body> 
<input type="text" value="" name="test" id="text_input"> 
<iframe src="iframe.html"> 
</body> 
</html> 

而且Iframe.html的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns:fb="http://ogp.me/ns/fb#"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>iframe</title> 
<script> 
function getValue(text) { 
    document.getElementById('text_input').value = text; 
} 
</script> 
</head> 
<body> 
<a href="#" onclick="getValue('test');">Click</a> 
</body> 
</html> 

當在標籤上點擊,值 「文本」 不能在from iframe to index.html,通過如何修理它 ?

回答

1

使用parent獲得iframe的父窗口。試試這個

function getValue(text) { 
    parent.document.getElementById('text_input').value = text; 
} 
相關問題