如何使用Javascript访问iframe元素? [英] How can I access iframe elements with Javascript?

查看:98
本文介绍了如何使用Javascript访问iframe元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网页,一个iframe里有一个texarea。我需要从其子页面javascript中读取此textarea的值。目前,在javascript中使用 window.parent.getelementbyID()。value ,我可以获取父页面中所有控件的值,除了iframe中的textarea。 / p>

我的父页面中的框架ID和框架名称在运行时更改,因此我们无法使用框架ID /框架名称作为参考。

解决方案

如果你有HTML

 < form name =formname.... id =form-first> 
< iframe id =onesrc =iframe2.html>
< / iframe>
< / form>

和JavaScript

  function iframeRef(frameRef){
return frameRef.contentWindow
? frameRef.contentWindow.document
:frameRef.contentDocument
}

var inside = iframeRef(document.getElementById('one'))

里面现在是对该文档的引用,所以你可以做 getElementsByTagName('textarea')和任何你喜欢的,这取决于iframe的src里面。


I have a webpage where there is a texarea within a iframe. I need to read the value of this textarea from its child page javascript. Presently by using window.parent.getelementbyID().value in the javascript, I am able to fetch values of all controls in the parent page except the textarea within the iframe.

The frame id and frame name in my parent page changes in runtime, hence we cannot use the frame id/frame name for reference.

解决方案

If you have the HTML

<form name="formname" .... id="form-first">
    <iframe id="one" src="iframe2.html">
    </iframe>
</form>

and JavaScript

function iframeRef( frameRef ) {
    return frameRef.contentWindow
        ? frameRef.contentWindow.document
        : frameRef.contentDocument
}

var inside = iframeRef( document.getElementById('one') )

inside is now a reference to the document, so you can do getElementsByTagName('textarea') and whatever you like, depending on what's inside the iframe src.

这篇关于如何使用Javascript访问iframe元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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