JavaScript中iframe实现局部刷新的几种方法汇总

Iframe是一种嵌入网页的框架形式,Web页面可以通过更改嵌入的部分,达到部分内容刷新,通过本文和大家一起学习iframe实现局部刷新的几种方法汇总,对iframe局部刷新相关知识感兴趣的朋友一起学习吧

Iframe是一种嵌入网页的框架形式,Web页面可以通过更改嵌入的部分,达到部分内容刷新。

Iframe的用法与普通的标签元素DIV类似,可以指定在页面中嵌入的位置、颜色、界面布局等

一、iframe实现局部刷新方法一

<script type="text/javascript"> $(function(){ $("#a1").click(function(){ var name= $(this).attr("name"); $("#iframe").attr("src",name).ready(); }) $("#a2").click(function(){ var name= $(this).attr("name"); $("#iframe").attr("src",name).ready(); }) }) </script> <a href="#">1</a> <a href="#">2</a> <iframe src=""></iframe>

当点a1时在iframe里显示a1.html的内容,点a2时在iframe里显示a2.html的内容

二、iframe实现局部刷新的方法二

<a href="https://www.jb51.net/a1.html" target="i">1</a> <a href="https://www.jb51.net/a2.html" target="i">2</a> <iframe src=""></iframe>

备注: <form> 同样也有target属性,作用和<a>一样 这个方式如果<from>或<a>提交到某个Action中再跳转到a1.html中效果一样,如果在Action中有req.set或session.set,最后在iframe中同样可以显示出来。

三:iframe实现局部刷新的方法三:

<iframe src="https://www.jb51.net/article/1.htm"></iframe>

方案一:用iframe的name属性定位

<input type="button" value="Button">

<input type="button" value="Button">

方案二:用iframe的id属性定位

<input type="button" value="Button">

方案三:当iframe的src为其它网站地址(跨域操作时)

<input type="button" value="Button">

方案四:通过和替换iframe的src来实现局部刷新

可以用document.getElementById("iframname").src=""来进行iframe得重定向;

示例代码如下:test.html

<!DOCTYPE html> <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 partRefresh() { document.getElementById("iframe1Id").src = "https://www.jb51.net/a2.html"; // 方法一: 通过和替换iframe的src来实现局部刷新 } </script> </head> <body> <table> <tr><td>方格1</td><td>方格2</td> <td>方格3</td> </tr> <tr> <td> <iframe src="https://www.jb51.net/a1.html"></iframe> </td> <td> <iframe src="https://www.jb51.net/a2.html"></iframe> </td> <td> <iframe src="https://www.jb51.net/a3.html"></iframe> </td> </tr> </table> <br> <br> <input type="button" value="IFRAME局部刷新"> </body> </html>

以上内容给大家介绍了JavaScript中iframe实现局部刷新的几种方法汇总,希望大家根据自己的需求选择适合自己的,有任何问题欢迎给我留言,谢谢!

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgwjpg.html