iframe局部刷新的两种实现方法

本文介绍两种使用iframe实现页面局部刷新的方法。方法一通过JavaScript监听链接点击,并改变iframe的src属性来加载新内容;方法二利用<a>标签的target属性指向iframe,直接实现局部刷新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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="#" id="a1" name="a1.html">1</a>
<a href="#" id="a2" name="a2.html">2</a>
<iframe src="" id="iframe"></iframe>

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


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

1
2
3
<a href="a1.html" id="a1" name="a1.html" target="i">1</a>
<a href="a2.html" id="a2" name="a2.html" target="i">2</a>
<iframe src="" id="iframe" name="i"></iframe>

 

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值