简体   繁体   English

如何使用JavaScript从iframe中添加价值?

[英]How to add value from a iframe using javascript?

I have a sample code: in index.html 我有一个示例代码:在index.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>index</title>  
</head>  
<body>
<input type="text" value="" name="test" id="text_input">
<iframe src="iframe.html">
</body> 
</html>

And iframe.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>

When click on a tag, value "text" can not be passed on from iframe to index.html , how to fix it ? 单击代码时,无法将值“文本” from iframe to index.html传递from iframe to index.html ,该如何解决?

Use parent to get parent window of iframe . 使用parent获取iframe父窗口。 Try this 尝试这个

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM