2012-08-17 155 views
1

有一個遺留網站,我正在處理,並且當點擊某個鏈接時我需要換出徽標圖像。這適用於Firefox和Chrome,但不適用於IE。jQuery選擇器在IE中不工作

要添加到問題的複雜性,包含腳本的頁面嵌套3個框架集內。鏈接與腳本位於同一個框架集中,但徽標位於頂部框架中。

$('a.reset-logo').click(function() { 
    var img = '../images/img1.gif'; 

    var $img = $('img.header-image', window.parent.top.frames[0].document); 

    //testing 
    //shows the correct src in chrome/firefox -- undefined in IE  
    //alert($img.attr('src')); 

    $img.attr('src', img); 
}); 

是的,我必須保持幀的使用。這不是重寫,只是維護問題。我一直在我的頭上撞牆,時間太長了。

我試圖改變的背景下window.top.frames [0] .document同樣的結果,在一些其他之中。問題在於選擇者,我似乎無法確定它是什麼。

+0

什麼版本的IE? – Kpower 2012-08-17 15:50:29

+0

這隻對IE 8.0+以上版本 – jsmith 2012-08-17 15:51:26

回答

0

我能夠得到它的工作通過將我的腳本更改爲以下內容:

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('a.reset-logo').click(function (e) { 
      var theFrame = window.parent.top.frames[0].document; 
      var theSrc = '../images/img1.jpg'; 

      $(theFrame).find('img.header-image').attr('src', theSrc); 
     }); 
    }); 
</script> 
0

請問http://jsfiddle.net/TJVuY/4/有幫助嗎?我拿走了window.parent.top.frames [0] .document-它不需要,因爲它是對文檔本身的侵蝕,現在它可以在IE 8中工作。

+0

這沒有任何幫助。腳本所在的文檔位於嵌套框架集中。需要更新的元素從觸發事件的文檔起上升兩個級別。 – jsmith 2012-08-20 13:21:38