Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

jquery操作iframe(齐全版,经IE6&IE6+、firefox、chrome测试通过)

$
0
0
  1. 父窗口获取子窗口元素  
    $('@selector', window.frames['@iframeID'].document)
  2. 子窗口获取父窗口元素  
    $('@selector', parent.document)
  3. 子窗口获取子窗口元素  
  4. $('@selector', parent.window.frames['@iframeID'].document)
  5. 附注:出于安全考虑,浏览器不允许脚本跨域访问iframe,谷歌浏览器离线状态下认为脚本访问iframe为跨域访问(跟cookie类似,离线状态下不允许使用cookie)
例子如下:
主窗口:(index.html)
<html>
	<head>
		<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
		<script type="text/javascript">
			$(function(){
				$('#test-button').click(function(){
					//获取左边iframe的元素(父窗口获取子窗口元素 )
					alert($('#test-button', window.frames['left'].document).text());
					//获取右边iframe的元素(父窗口获取子窗口元素 )
					alert($('#test-button', window.frames['right'].document).text());
				})		
			})
		</script>
	</head>
	<body>
		<iframe id="left"  width="30%" height="50%" src="left.html"></iframe>
		<iframe id="right" width="30%" height="50%" src="right.html"></iframe>
		<button id="test-button">test-button-in-index</button>
	</body>
</html>
左边iframe:(left.html)
<html>
	<head>
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
	<script type="text/javascript">
		$(function(){
			$('#test-button').click(function(){
				//获取主窗口iframe的元素(子窗口获取父窗口元素)
				alert($('#test-button', parent.document).text());
				//获取主窗口右边iframe的元素(子窗口获取子窗口元素)
				alert($('#test-button', parent.window.frames['right'].document).text());
			})		
		})
	</script>
	</head>
	<body>
		<button id="test-button">test-button-in-left</button>
	</body>
</html>
右边iframe:(right.html)
<html>
	<head></head>
	<body>
		<button id="test-button">test-button-in-right</button>
	</body>
</html>



作者:liuyanzhi08 发表于2013-8-29 19:01:08 原文链接
阅读:54 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>