2016-07-05 82 views
-2

我有这个div:滚动事件不工作的JQuery

<div id="inbox-prof-load-msg" class="message-content row nice-scroll">      
    <?php include_once('inbox-chat-msg.php'); ?>        
</div> 

而且我试图让这个div的Scroll()

$('div#inbox-prof-load-msg').scroll(function() { 
    console.log('Scrolled') 
}); 

为什么我没有收到Scroll事件?我做了一个.click()调用来测试,并且Javascript也没有得到div上的点击事件。 有人知道如何解决它?

注意:元素div#inbox-prof-load-msg被动态添加到DOM。

谢谢。

+0

? –

+3

动态添加'inbox-prof-load-msg' div吗? –

+1

元素是否实际滚动? –

回答

4

使用事件代表团当你处理新鲜DOM(动态添加到DOM元素)on()

​​

希望这有助于。

var div = "<div id='inbox-prof-load-msg'> test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br>test<br></div>"; 
 

 
$('body').append(div); 
 

 
$('#inbox-prof-load-msg').on('scroll',function (event) { 
 
    console.log('Scrolled'); 
 
}); 
 

 
$('body').on('click', '#inbox-prof-load-msg', function() { 
 
    console.log('Clicked') 
 
});
#inbox-prof-load-msg{ 
 
    height:150px; 
 
    overflow: scroll; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

这jQuery的版本,您使用的
+0

嘿,我想'滚动',但不工作,但'点击'的作品,你知道为什么吗? –