JS跨文件控制多个iframe页面的实现方案
作者:木棉时间:2026-10-22 11:22:32浏览:100
本文介绍利用SHTML或ASP的include指令将连接iframe的代码整合到被包含文件中,从而实现通过单个JS函数同时控制多个iframe页面刷新的技巧。
JS跨文件控制多个iframe页面的实现方案
一种可行的方案是建议将涉及连接的iframe部分改为通过include指令包含的文件,同时确保文件名后缀设置为shtml或asp。具体实现代码如下:
<!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=gb2312" />
<title>无标题文档</title>
<meta http-equiv="refresh" content="3;URL=#" />
</head>
<script type="text/javascript">
function change(){
var a = document.getElementById("iframea");
var b = document.getElementById("iframeb");
a.setAttribute("src","http://www.163.com");
b.setAttribute("src","http://www.sina.com");
}
</script>
<body>
<!--#include file="z.html" -->
<iframe id="iframea" src="http://www.baidu.com/" width="450" height="300"></iframe>
<iframe id="iframeb" src="http://www.g.cn/" width="450" height="300">></iframe>
</body>
</html>
被包含文件中的相关代码示例如下:
<a href="#" onclick="change()">其中内容点击链接更新两个iframe</a>
总结
以上内容介绍了关于JS跨文件同时控制多个iframe页面的应用技巧,希望能对读者有所帮助。如有任何疑问,欢迎留言,小编会及时回复。在此感谢大家对本网站的支持!
作者最新文章
JS合并多个数组的三种方法:concat、spread与flat
2026-11-05 17:39:45
JavaScript怎么准确判断数据类型:typeof、instanceof与Object.prototype.toString
2026-11-05 17:09:21
JS中将数组和对象混合组装为JSON字符串
2026-11-05 16:41:42
JS运算符优先级与结合性的区别是什么?
2026-11-05 13:22:02
JS Map怎么只遍历Key或Value
2026-11-05 13:00:36
