我的脚本只能在控制台上运行 [英] my script only working from console

查看:78
本文介绍了我的脚本只能在控制台上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本放在我的html文档中。

I have a script that I put inside my html doc.

当我加载html页面时脚本无效,但当我把脚本放入控制台然后脚本正在制作我想要的html文档效果。

when I load the html page the script is not working,but when I put the script inside the console then the script is working an making the effect I want on the html doc.

这里是脚本,我做错了什么?

here is the script,what i'm doing wrong?

var maxRows = 10;
$('.table').each(function() {
var cTable = $(this);
var cRows = cTable.find('tr:gt(0)');
var cRowCount = cRows.size();

if (cRowCount < maxRows) {
    return;
}

cRows.each(function(i) {
    $(this).find('td:first').text(function(j, val) {
       return (i + 1) + " - " + val;
    }); 
});

cRows.filter(':gt(' + (maxRows - 1) + ')').hide();


var cPrev = cTable.siblings('.prev');
var cNext = cTable.siblings('.next');

cPrev.addClass('disabled');

cPrev.click(function() {
    var cFirstVisible = cRows.index(cRows.filter(':visible'));

    if (cPrev.hasClass('disabled')) {
        return false;
    }

    cRows.hide();
    if (cFirstVisible - maxRows - 1 > 0) {
        cRows.filter(':lt(' + cFirstVisible + '):gt(' + (cFirstVisible - maxRows - 1) + ')').show();
    } else {
        cRows.filter(':lt(' + cFirstVisible + ')').show();
    }

    if (cFirstVisible - maxRows <= 0) {
        cPrev.addClass('disabled');
    }

    cNext.removeClass('disabled');

    return false;
});

cNext.click(function() {
    var cFirstVisible = cRows.index(cRows.filter(':visible'));

    if (cNext.hasClass('disabled')) {
        return false;
    }

    cRows.hide();
    cRows.filter(':lt(' + (cFirstVisible +2 * maxRows) + '):gt(' + (cFirstVisible + maxRows - 1) + ')').show();

    if (cFirstVisible + 2 * maxRows >= cRows.size()) {
        cNext.addClass('disabled');
    }

    cPrev.removeClass('disabled');

    return false;
});

});

推荐答案

尝试用这个包装整个事情:

Try wrapping the whole thing with this:

$(document).ready(function () { /* existing code */ });

浏览器可能在页面加载之前执行代码,因此在元素存在之前。

The browser may be executing your code before the page has loaded and therefore before the elements exist.

这篇关于我的脚本只能在控制台上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆