jquery垮頁面事件傳遞

我有一個需求須要在top頁面廣播一個事件,而後全部的iframe 或 frame中的頁面接收到這個事件後作出對應的響應,實現基於jquery庫。 jquery

代碼以下: this

在top頁面裏這麼寫 spa

$("iframe,frame").each(function(){
      $(this.contentWindow.document).trigger("idle")
})



在子頁面裏這麼寫 code

$(document).bind("idle",callback)



乍一看 貌似是可行的,可是其實是不會生效的。 對象

爲何? 事件

雖然 事件都是綁定到了同一個 document對象上,可是實際上 用以進行事件出發的jquery對象自己($)和進行監聽綁定的jquery對象不是同一個,因此應該改爲下面這種。 iframe

$("iframe,frame").each(function(){
     var sub_jquery=this.contentWindow.$;
     if(sub_jquery){
        sub_jquery(this.contentWindow.document).trigger("idle");
     }
})
相關文章
相關標籤/搜索