我有这个div:
<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上获得click事件。
有人知道如何解决它吗?
注意:元素div#inbox-prof-load-msg
会动态添加到DOM中。
感谢。
答案 0 :(得分:4)
在处理新DOM(动态添加到DOM中的元素)时使用事件委派 on()
:
$('#inbox-prof-load-msg').on('scroll',function (event) {
console.log('Scrolled');
});
$('body').on('click', 'div#inbox-prof-load-msg', function() {
console.log('Clicked')
});
希望这有帮助。
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>