简体   繁体   English

为什么我的JS / JQuery代码只能在Web浏览器的控制台中使用?

[英]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.

相关问题 为什么有些jquery代码只有在我的html-body部分结尾时才能工作? - Why does some jquery code only work if it's at the end of my html-body section? JS代码无法在Web浏览器上正常运行 - JS Code does not work on web browser as intended 为什么在使用jQuery显示数据时我的循环不起作用,但在浏览器控制台中却如此? - Why doesn't my loop work when displaying data using jQuery but does in browser console? 为什么我的 $(&quot;.selector&quot;).show(900); 代码只在浏览器控制台中启动,但从不在助手中启动? - Why does my $(".selector").show(900); code only fire-up in the browser console, but never in the helper? 为什么此Javascript在浏览器控制台中有效,但在Selenium的JavascriptExecutor中却无效? - Why does this Javascript work in the browser console but not in Selenium's JavascriptExecutor? 为什么我的 JS 代码中的 clearForm() 不起作用? - Why does not work clearForm() in my code in JS? 为什么我的网站的滑块只能在Mac(相同的浏览器)上正常工作? - Why does my site's slider work properly only on Mac (same browser)? 为什么我的preventDefault在Chrome浏览器jquery中不起作用? - Why does my preventDefault not work in Chrome browser jquery? 为什么此jQuery只能作为小提琴而不在我的网页上使用? - Why does this jQuery work as a fiddle but not on my web page? 为什么此代码仅在某些JS执行程序中有效? - Why does this code only work in certain JS executors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM