[英]Why does my JS/JQuery code only work in a web browser's console?
The $(element).scroll(function(){}); $(element).scroll(function(){}); function isn't working for me when I put it into a js file but when I enter it into the console (just the scroll func), it works just fine. 当我将其放入js文件时,该函数对我不起作用,但是当我将其输入到控制台中时(只是滚动功能),它就可以正常工作。
I'm trying to do a scrolling pagination. 我正在尝试进行滚动分页。
I've looked through my other js files and the only thing that I think could be conflicting is another one of the files has a $(document).ready(function(){}) but I'm pretty sure that's not the problem. 我查看了我的其他js文件,我认为可能唯一矛盾的是另一个文件中有一个$(document).ready(function(){}),但我敢肯定这不是问题所在。 I'm also using Dropkick to make pretty dropdowns but I doubt that's it either. 我也在使用Dropkick进行漂亮的下拉菜单,但我也怀疑是这样。
Here's the code, almost verbatim. 这是几乎逐字记录的代码。 It's basic for now until I can figure out how to get it to load. 在我弄清楚如何加载之前,这是现在的基本操作。
$('#main').scroll(function(){
if(($('#main').prop('scrollHeight'))==
($('#main').scrollTop()+$(document).height()-10)){
//^there's a strange 10px empty space that needs to be accounted for
$('#loading').show();
$('#main').css('overflow','hidden');
addMore();
}
});
$(document).ready(function(){
addMore();
});
addmore.counter=0;
function addMore(){
$.ajax({
async: 'true',
url: 'http://mywebsite.com/bc/get',
type: 'post',
data: ({'offset':(addmore.counter)}),
success: function(data) {
$('#scrollingpagination').append(data);
$('#loading').hide();
$('#main').css('overflow','scroll');
addmore.counter++;
}
});
}
And here's the HTML (not verbatim, but same idea) 这是HTML(不是逐字记录,而是相同的想法)
<!--I'm only including the "main" div that shows the content.-->
<div id='main'>
<div id='scrollingpagination'></div>
<div id='loading'></div>
</div>
Thanks guys, I really appreciate it! 谢谢大家,我真的很感激!
try keeping 尝试保持
$('#main').scroll(function(){
......
})
inside document.ready
. 在document.ready
内部。 i think it was called before the dom was ready 我认为这是在dom准备好之前被调用的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.