JS跨文件控制多个iframe页面的实现方案

作者:木棉

时间:2026-10-22

浏览:99

本文介绍利用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页面的应用技巧,希望能对读者有所帮助。如有任何疑问,欢迎留言,小编会及时回复。在此感谢大家对本网站的支持!

实用工具推荐

电子包浆

绿化、压缩、噪点和虚拟水印,自由调节做旧程度。

使用 ›
图片压缩

选择画质和输出格式,预览压缩前后的大小。

使用 ›
相关文章