【问题标题】:Getting a DIV to auto load with jQuery使用 jQuery 让 DIV 自动加载
【发布时间】:2009-09-12 01:28:18
【问题描述】:

我之前尝试过更改 ID 属性。如果请求 === 0,有没有办法让下面的内容自动加载?而不是有点击功能。

<script language="javascript">
var requests = 10;

function request(self)
{if(self.href != "#")
requests -= 1;

if(requests === 0)

var pageid = function () {
    var thisID = null;
    $('#step').click(function () {
        thisID = this.id;
        $('#content').animate({
            height: getPageHeight()
        },
        700, function () {
            $('#next-page').fadeIn(500);
            $('#content-' + thisID).fadeIn(500, function () {});
        });
        return false;
    });
};
$(window).load(pageid);
function getPageHeight() {
    var windowHeight;
    if (self.innerHeight) windowHeight = self.innerHeight;
    else if (document.documentElement && document.documentElement.clientHeight) windowHeight = document.documentElement.clientHeight;
    else if (document.body) windowHeight = document.body.clientHeight;
    return windowHeight;
}}
</script>

【问题讨论】:

  • if(requests === 0) 是故意的吗?很臭。

标签: javascript jquery load


【解决方案1】:

尝试将所有代码包装在此:

$(document).ready(function() { 
    // your code
}

当代码在页面加载时运行时,您会遇到您要查找的内容尚未准备好使用(甚至可能尚未下载)的情况。因此,请确保在运行代码之前等到页面加载完毕。

编辑

可能是“$(window).load(pageid);”只是错误的代码,但如果仍然无法正常工作,您将必须更具体地说明您的目标。

类似这样的:

<script language="javascript">
var requests = 10;

$(document).ready(function() { 
  // Your code here
  // $(window).load(pageid);
});

function request(self)
{if(self.href != "#")
requests -= 1;

if(requests === 0)

var pageid = function () {
    var thisID = null;
    $('#step').click(function () {
        thisID = this.id;
        $('#content').animate({
            height: getPageHeight()
        },
        700, function () {
            $('#next-page').fadeIn(500);
            $('#content-' + thisID).fadeIn(500, function () {});
        });
        return false;
    });
};

function getPageHeight() {
    var windowHeight;
    if (self.innerHeight) windowHeight = self.innerHeight;
    else if (document.documentElement && document.documentElement.clientHeight) windowHeight = document.documentElement.clientHeight;
    else if (document.body) windowHeight = document.body.clientHeight;
    return windowHeight;
}}
</script>

【讨论】:

  • 我不确定您的评论的真正含义。你不需要包装函数,你只需要包装浏览器一发现它就会运行的代码。
【解决方案2】:

尝试在加载事件中使用匿名函数,如下所示:

$(window).load(function() {
   if(requests === 0) 
      pageid(); // or whatever you need
});

但首先从您的代码中删除 if(requests === 0) 条件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 2012-03-26
    相关资源
    最近更新 更多