2015-09-07 22 views
1

我怎樣才能讓我的jQuery腳本,當一個iframe加載運行一次運行一次?這是我的代碼。請jQuery腳本時的iframe裝載

我的代碼

$(document).ready(function(){ 

    //setup a counter to keep our place and cache a selection to the letter wrappers 
    var counter = 0, 
     $chars = $(".test").children(); 

    //setup an interval to animate a letter every so often 
    setInterval(function() { 

     //select the current letter wrapper and animate it 
     $chars.eq(counter).effect("bounce", {times:1}, 500); 

     //increment the counter so we animate the next letter next time around 
     counter++; 

     //check if the counter still relates to an index of the $chars collection 
     if (counter >= $chars.length) { 
      counter = 0; 
     } 
    }, 250); 

}); 

見我fiddle

我需要它來運行此腳本一次我的id爲playing_song iframe中加載時。它可以在任何時候。

我試過了。

我一直在使用.ready嘗試,但顯然它只會顯示它時,它的準備,而不是當它的加載。

我在做的事情是製作一個帶有靜態div的動畫的div。但它不能動態更改,因爲iframe可以隨時加載。此方法僅適用於已刷新的父項。

什麼我後

所有我想要的代碼做的是運行一次當IFRAME playing_song加載,它隨時可能發生,而且不止一次。

有關如何實現這一點的任何建議?

回答

1

的IFRAME你應該能夠用普通的JavaScript來做到這一點。 考慮這個HTML:

<div class="test"> 
    <span>M</span> 
    <span>u</span> 
    <span>s</span> 
    <span>i</span> 
    <span>f</span> 
    <span>y</span> 
</div> 

<iframe id="foo" href="https://youtube.com" width="300" height="250"></iframe> 

那麼你可以使用這個JavaScript時, '#foo' 開始加載做一些事情:

var frame_to_load = document.getElementById("foo"); 
frame_to_load.onload = function() { 
    // your js here 
} 

或使用jQuery:

$('#foo').on('load', function() { 
    // your js here 
}); 

如果你有訪問您的iframe,您也可以從iframe中運行$(document).load(function(){...});內的js。

希望這會有所幫助。

0

使用$(document).load訪問負載情況下,在過程中