簡體   English   中英

跨瀏覽器解決方案,用於檢查Null Iframe

[英]Cross browser solution for checking Null Iframes

我已經有很長時間了,但我一直在苦苦掙扎,最后我不得不發布一個查詢,因為到目前為止我還沒有收到任何相關的答案。

我需要檢查Null Iframe,然后調用一個函數。 我正在尋找以下三行代碼的替代品,以便能夠在所有瀏覽器中工作,而不僅僅是在IE中。

if (this.document.frames != null ) {
    for (var i=0;i<this.document.frames.length;i++){
        if (this.document.frames[i] != null ) {

PS:我不能使用getElementbyId或getElementsByTagName,因為代碼中的IFrame是動態創建的。 抱歉,據我所知,但是我不熟悉Javascript,現在似乎嘗試了太多事情:(

非常感謝。

您仍然可以將getElementbyIdgetElementsByTagName用於動態添加的元素,只要在將元素添加到DOM之后尋找它們即可。

換句話說,您只有在尋找它們的時候找到它們。

var iframes = document.getElementsByTagName('iframe'); // notice this is not 'frame'

var length = iframes.length;
var currentIFrame;
var counter;

for (counter = 0; counter < length; counter += 1) {
    currentIFrame = iframes[counter];

    // do stuff with currentIFrame
}

http://jsfiddle.net/fuEsw/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM