将输入文本从iframe移动到主窗口 [英] move input text from iframe to main window

查看:90
本文介绍了将输入文本从iframe移动到主窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面上有一个iframe,允许我们上传图片,一旦上传,它会在文本输入字段中显示文件的网址。

I have an iframe on a page that allows us to upload an image, once it's uploaded it displays the url of the file in a text input field.

On页面的主要部分,我们有textarea,我们在那里写新闻帖子。我想要一种方法,以便我可以在url字段旁边的iframe上添加一个链接,点击后会自动将文本插入到页面主要部分的textarea中。

On the main part of the page, we have our textarea where we write up our news posts. I'd like a way so that I can add a link on the iframe, next to the url field, that when clicked will automatically insert the text in there into the textarea on the main part of the page.

我对javascript相对较新,所以在使用iframe时我不知道如何做到这一点。

I'm relatively new to javascript, so I wasn't sure how to do this when using an iframe.

谢谢!

推荐答案

Main.htm

<html>
<head>
<script>
getText = function(s)
{
alert(s);
}
</script>
</head>
<body>
<iframe src="otherFrame.htm"></iframe>
</body>
</html>

otherFrame.htm

otherFrame.htm

<html>
<head>
<script>
botherParent = function()
{
    parent.getText(document.getElementById("textInput").value);
};
window.onload = function()
{
}
</script>
</head>
<body>
<input type="text" id="textInput" />
<span onclick="botherParent()">Stuff goes here</span>
</body>
</html>

基本上,在子内联框架中,使用parent访问父窗口对象。
全局变量存储在窗口中,全局函数也是如此。因此,如果在父级中定义全局变量foo,则可以使用带有子框架的parent.foo访问它。

Basically, in the child inline frame, use parent to access the parent's window object. Global variables are stored in window, as are global functions. Because of this, if you define a global variable "foo" in parent, you can access it with parent.foo with your child frame.

希望有帮助!

这篇关于将输入文本从iframe移动到主窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆