首頁 > web前端 > js教程 > 主體

JavaScript中iframe實作局部刷新的幾種方法總結_javascript技巧

WBOY
發布: 2016-05-16 15:21:33
原創
1598 人瀏覽過

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="#" 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實作局部刷新的方法二

<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> 
登入後複製

備註:

同樣也有target屬性,作用和一樣這個方式如果提交到某個Action中再跳到a1.html中效果一樣,如果在Action中有req.set或session.set,最後在iframe中同樣可以顯示出來。

三:iframe實作局部刷新的方法三:

<iframe src="1.htm" name="ifrmname" 
id="ifrmid"></iframe>
登入後複製

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

<input type="button" name="Button" value="Button" onclick="document.frames('ifrmname').location.reload()">
登入後複製

<input type="button" name="Button" value="Button" onclick="document.all.ifrmname.document.location.reload()">
登入後複製

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

<input type="button" name="Button" value="Button" onclick="ifrmid.window.location.reload()">
登入後複製

方案三:當iframe的src為其它網站位址(跨域操作時)

<input type="button" name="Button" value="Button" onclick="window.open(document.all.ifrmname.src,'ifrmname','')">
登入後複製

方案四:透過和替換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 = "a2.html"; // 方法一: 通过和替换iframe的src来实现局部刷新
  }
 </script>
 </head>
 <body>
  <table border="1" width="90%" align="center">
   <tr
    style="background: #F0F0E4"><td>方格1</td><td>方格2</td> <td>方格3</td>
   </tr>
   <tr>
    <td>
     <iframe src="a1.html" id="iframe1Id" name="iframe1Name" width="100%"></iframe>
    </td>
    <td>
     <iframe src="a2.html" id="iframe2Id" name="iframe2Name" width="100%"></iframe>
    </td>
    <td>
     <iframe src="a3.html" id="iframe3Id" name="iframe3Name" width="100%"></iframe>
    </td>
   </tr>
  </table>
  <br>
  <br>
  <input type="button" value="IFRAME局部刷新" style="margin-left: 70px;" onclick="partRefresh();">
 </body>
</html> 
登入後複製

以上內容為大家介紹了JavaScript中iframe實現局部刷新的幾種方法匯總,希望大家根據自己的需求選擇適合自己的,有任何問題歡迎給我留言,謝謝!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!