2015-10-02 50 views
0

我們有一個應用程序,我們無法觸摸源代碼。這就是爲什麼我不能將JavaScript代碼注入iframe。我試圖操縱的jQuery和JavaScript的圖像按鈕SRC如下:更改動態創建的iframe的內容

// there isn't id of the iframe so I used to catch by tag 
$("input:image", window.parent.frames[0].document).each(function() { 
    $(this).attr("src", "http://" + document.location.hostname + "/theme/img/buttonDownArrow.png") 
}); 

// other attempt 
document.getElementsByTagName('iframe').onload = function() { 
    $("input:image", window.parent.frames[0].document).each(function() { 
     $(this).attr("src", "http://" + document.location.hostname + "/theme/img/buttonDownArrow.png") 
    }); 
} 

,但沒有發生。在iframe加載正常後,當我在控制檯中嘗試以上代碼時。我把源代碼放在頁面的末尾,但沒有任何事情發生。反正有沒有操縱加載的iframe內容?

回答

0

我用下面的方法解決了這個問題。 Thanks Martin for the snippet

$('[data-dojo-attach-point="iframeViewer"]').ready(function() { 
     $('#form1').contents().find('input:image').each(function() { 
     $(this).attr('src', 'http://' + document.location.hostname + '/theme/img/buttonDownArrow.png'); 
     $(this).addClass('modifyComboArrowButton'); 
     $(this).hover(
     function() { 
      $(this).attr('src', 'http://' + document.location.hostname + '/theme/img/buttonDownArrow_hover.png') 
     }, 
     function() { 
      $(this).attr('src', 'http://' + document.location.hostname + '/theme/img/buttonDownArrow.png') 
     }) 
     }); 

     $('.ComboBoxDelButton').each(function() { 
     $(this).addClass('modifyComboDeleteButton'); 

     $(this).hover().addClass('modifyComboDeleteButton:hover'); 
     }); 
    }) 

這裏是棘手的部分:

$('#form1').contents().find('input:image') 
0

這可能取決於在不同的域。如果iframe與加載在iframe中的頁面位於不同的域中,則在試圖從「上方」操作代碼時,可能會遇到安全問題,就像您試圖從「下方」影響框架一樣。

+0

它是在同一個域。 –