jQuery iframe滚动事件(IE)

无法在Internet Explorer 7中收听滚动事件。

我试过了:

$("#myIframe").scroll(function() { alert('hi'); }) 

为FF工作:

 $($("#myIframe").contents().get(0)).scroll(function() { alert('hi'); }) 

获得按键工作:

 $($("#myIframe").contents().get(0)).keydown(function() { alert('hi'); }) 

尽管我喜欢jQuery。 我不能让这个工作。 但是,我在普通的旧javascript中试过这个,它在IE,FF,Safari和Chrome中运行得很好。

  

编辑:使用window.load()时,以下适用于FF,Safari和Chrome。 使用document.ready时它只适用于FF。 无论出于何种原因,它都无法在IE8中运行。

 $(window).load(function(){ $($('#myIframe').contents()).scroll(function(){ alert('frame scrolled in jquery'); }); }); 

我知道这是一个旧线程,但有些人会发现它很有用。

$(document).scroll()可以用$(window).scroll()替换,到目前为止它对我$(window).scroll()

试试这个:

在遍历嵌套浏览上下文的dom之前必须发生两件事。

您需要知道iframe存在,并处理文档就绪事件。

而且您需要确保iframe已加载。

即:

  $(document).ready(function(){ // #page is the id of the iframe $('#page').load(function(){ // $(this)[0].contentWindow is the window of your nested browsing context/ iframe $($(this)[0].contentWindow).scroll(function(){ console.log($(this).scrollTop()); }); }); }); 

需要注意的一点是,这绝对不适用于Firefox中的跨浏览器。

把它放在父母身上:

 var childScrollHandler = function () { alert('Scrolling going on'); } 

然后把它放在iframe内容上:

 $(document).bind('scroll', function(ev){ parent.childScrollHandler(ev); }); 

用你试图听的任何元素替换$(document)