代码之家  ›  专栏  ›  技术社区  ›  homework

使用jquery使DIV自动加载

  •  0
  • homework  · 技术社区  · 15 年前

    我之前尝试过更改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>
    
    2 回复  |  直到 15 年前
        1
  •  1
  •   John Fisher    15 年前

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

    $(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
  •  1
  •   Sergei    15 年前

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

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

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