2016-07-05 90 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

嘿,我想'滾動',但不工作,但'點擊'的作品,你知道爲什麼嗎? –