`

从父页面读取和操作iframe中内容方法

阅读更多

基本的操作方法:

 

       document.frames("frame_id").document.action;

 

其中,frame_id是该父页面需要进行操作的iframe的id,action是iframe中的相关操作。

从该方法中,可以看出 document.frames("frame_id")是用来从父页面中获取iframe的id的,而后面的document.action同一般的脚本对页面元素操作一样,具体举个例子来说明一下,其中父页面引用iframe部分如下:

 

父页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function iframe_test(){
	if(document.frames("test_iframe").document.getElementById("username").value==""){
    	alert("请输入用户名!");
    }
    if(document.frames("test_iframe").document.getElementById("descript").value==""){
		document.frames("test_iframe").document.getElementById("descript").value="descript";
	}
}
</script>
</head>
<body>
<iframe onload="iframe_test()" frameborder="0" scrolling="no" name="test_iframe" src="Untitled-2.html" width="100%" height="100%">
</iframe>
</body>
</html>

 

 

子页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<input type="text" id="username" /><br />
<input type="text" id="descript" /><br />
<input type="button" value="提交" />
</body>
</html>

 

此例描述了在父页面中读取iframe中元素以及在父页面中修改iframe中元素的属性。

通过此例,我们可以看出,在父页面中访问iframe中的各个元素与一般的访问页面元素无本质区别,无非是需要在父页面中事先获取需要处理的iframe对象,在获取iframe对象后,其操作基本没什么特别之处。

 

分享到:
评论
1 楼 moneyprosper 2011-12-07  
就只有IE支持,火狐和谷歌都不支持此种方法。正在寻找中。。。

相关推荐

Global site tag (gtag.js) - Google Analytics