js判斷iframe中元素是否存在的實現(xiàn)代碼
本文章來給大家介紹js判斷iframe中元素是否存在代碼,有需要了解的朋友可進入?yún)⒖肌?/p>
一、純原生態(tài)js實現(xiàn)方法,代碼如下:
<script> var bb = document.getElementById('PreviewArea').contentWindow.document.getElementById('aPic'); if( bb ) { } else { } //apic為子頁面Preview.aspx里面元素的Id </script> <body> <iframe name="PreviewArea" id="PreviewArea" scrolling="yes" width="100%" height="290" frameborder="1" src="Preview.aspx"></iframe> </body>
二、現(xiàn)在流行的jquery實現(xiàn)方法,代碼如下:
if($(window.frames["iframepage"].document).find('.l-grid-row-cell').length > 0){ alert(1); }else{ alert(2); }
以上代碼,判斷id為iframepage的iframe中css為1-grid-row-cell的元素是否存在。
附
Jquery取得iframe中元素的幾種方法
在iframe子頁面獲取父頁面元素
$('#objId', parent.document); // 搞定... 在父頁面 獲取iframe子頁面的元素 $("#objid",document.frames('iframename').document) $(document.getElementById('iframeId').contentWindow.document.body).html()
顯示iframe中body元素的內(nèi)容
$("#testId", document.frames("iframename").document).html(); 根據(jù)iframename取得其中ID為"testId"元素 $(window.frames["iframeName"].document).find("#testId").html()
2、用JS或jQuery訪問頁面內(nèi)的iframe,兼容IE/FF
注意:框架內(nèi)的頁面是不能跨域的!
假設(shè)有兩個頁面,在相同域下.
index.html 文件內(nèi)含有一個iframe:
<!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> </head> <body> <iframe src="iframe.html" id="koyoz" height="0" width="0"></iframe> </body> </html>
iframe.html 內(nèi)容:
<!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>iframe.html</title> </head> <body> <div id="test">www.dbjr.com.cn</div> </body> </html>
1. 在index.html執(zhí)行JS直接訪問:
document.getElementById('koyoz').contentWindow.document.getElementById('test').style.color='red'
通過在index.html訪問ID名為'koyoz'的iframe頁面,并取得此iframe頁面內(nèi)的ID為'test'的對象,并將其顏色設(shè)置為紅色.
此代碼已經(jīng)測試通過,能支持IE/firefox。
2. 在index.html里面借助jQuery訪問:
$("#koyoz").contents().find("#test").css('color','red');
此代碼的效果和JS直接訪問是一樣的,由于借助于jQuery框架,代碼就更短了.
另外,有網(wǎng)友提供了如下的示例:
用jQuery在IFRAME里取得父窗口的某個元素的值,只好用DOM方法與jquery方法結(jié)合的方式。
1. 在父窗口中操作 選中IFRAME中的所有單選鈕
$(window.frames["iframe1"].document).find("input:radio").attr("checked","true");
2. 在IFRAME中操作 選中父窗口中的所有單選鈕
$(window.parent.document).find("input:radio").attr("checked","true");
父窗口想獲得IFrame中的Iframe,就再加一個frames子級就行了,如:
$(window.frames["iframe1"].frames["iframe2"].document).find("input:radio").attr("checked","true")
相關(guān)文章
js定時器setInterval、clearInterval的使用方法舉例
Javascript的setTimeOut和clearInterval函數(shù)應(yīng)用非常廣泛,它們都用來處理延時和定時任務(wù),這篇文章主要給大家介紹了關(guān)于js定時器setInterval、clearInterval使用方法的相關(guān)資料,需要的朋友可以參考下2023-11-11javascript面向?qū)ο笾蚕沓蓡T屬性與方法及prototype關(guān)鍵字用法
這篇文章主要介紹了javascript面向?qū)ο笾蚕沓蓡T屬性與方法及prototype關(guān)鍵字用法,實例分析了prototype關(guān)鍵字在共享成員屬性與方法中的原理與使用技巧,需要的朋友可以參考下2015-01-01