JavaScript中iframe實(shí)現(xiàn)局部刷新的幾種方法匯總
Iframe是一種嵌入網(wǎng)頁的框架形式,Web頁面可以通過更改嵌入的部分,達(dá)到部分內(nèi)容刷新。
Iframe的用法與普通的標(biāo)簽元素DIV類似,可以指定在頁面中嵌入的位置、顏色、界面布局等
一、iframe實(shí)現(xiàn)局部刷新方法一
<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>
當(dāng)點(diǎn)a1時(shí)在iframe里顯示a1.html的內(nèi)容,點(diǎn)a2時(shí)在iframe里顯示a2.html的內(nèi)容
二、iframe實(shí)現(xiàn)局部刷新的方法二
<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>一樣 這個(gè)方式如果<from>或<a>提交到某個(gè)Action中再跳轉(zhuǎn)到a1.html中效果一樣,如果在Action中有req.set或session.set,最后在iframe中同樣可以顯示出來。
三:iframe實(shí)現(xiàn)局部刷新的方法三:
<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()">
方案三:當(dāng)iframe的src為其它網(wǎng)站地址(跨域操作時(shí))
<input type="button" name="Button" value="Button" onclick="window.open(document.all.ifrmname.src,'ifrmname','')">
方案四:通過和替換iframe的src來實(shí)現(xiàn)局部刷新
可以用document.getElementById("iframname").src=""來進(jìn)行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來實(shí)現(xiàn)局部刷新
}
</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>
以上內(nèi)容給大家介紹了JavaScript中iframe實(shí)現(xiàn)局部刷新的幾種方法匯總,希望大家根據(jù)自己的需求選擇適合自己的,有任何問題歡迎給我留言,謝謝!
- js檢測(cè)iframe是否加載完成的方法
- js實(shí)現(xiàn)防止被iframe的方法
- iframe里使用JavaScript控制主頁轉(zhuǎn)向的方法
- JS獲取iframe中l(wèi)ongdesc屬性的方法
- js下判斷 iframe 是否加載完成的完美方法
- js實(shí)現(xiàn)的真正的iframe高度自適應(yīng)(兼容IE,FF,Opera)
- JS獲取并操作iframe中元素的方法
- iframe子父頁面調(diào)用js函數(shù)示例
- js iframe跨域訪問(同主域/非同主域)分別深入介紹
- JavaScript實(shí)現(xiàn)iframe自動(dòng)高度調(diào)整和不同主域名跨域
相關(guān)文章
十分鐘打造AutoComplete自動(dòng)完成效果代碼
十分鐘打造山寨版谷歌AutoComplete,因?yàn)槭鞘昼姶蛟斐鰜淼?,所以只考慮表面效果,其他全部忽略,絕對(duì)的山寨。2009-12-12
JavaScript數(shù)據(jù)結(jié)構(gòu)之雙向鏈表和雙向循環(huán)鏈表的實(shí)現(xiàn)
本篇文章主要介紹了JavaScript數(shù)據(jù)結(jié)構(gòu)之雙向鏈表和雙向循環(huán)鏈表的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
JS實(shí)現(xiàn)點(diǎn)擊按鈕自動(dòng)增加一個(gè)單元格的方法
這篇文章主要介紹了JS實(shí)現(xiàn)點(diǎn)擊按鈕自動(dòng)增加一個(gè)單元格的方法,實(shí)例分析了javascript操作表格單元格的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
TypeScript實(shí)現(xiàn)字符串轉(zhuǎn)樹結(jié)構(gòu)的方法詳解
有一個(gè)多行字符串,每行開頭會(huì)用空格來表示它的層級(jí)關(guān)系,每間隔一層它的空格總數(shù)為2,如何將它轉(zhuǎn)為json格式的樹型數(shù)據(jù)?本文就跟大家分享下這個(gè)算法2022-09-09
TypeScript模塊與命名空間的關(guān)系和使用方法
在TypeScript中就像在EC5中一樣,任何包含頂級(jí)import或export的文件都被認(rèn)為是一個(gè)模塊,下面這篇文章主要給大家介紹了關(guān)于如何在TypeScript使用模塊與命名空間以及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下2023-03-03

