你获取的元素是<html>
scrollTop可能是多少????

解决方案 »

  1.   

    以下是我测试的代码:
    <HTML>
    <HEAD>
    <TITLE> New Document </TITLE>
    </HEAD><BODY id="theBody">
    <form action="#" method="post">
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <input type="text" name="__SCROLLPOS" id="__SCROLLPOS" value="10" onclick="javascript:alert(this.scrollLeft);">
    <input type="submit" value="submit">
    </form>
    <br>
    <script language='javascript'>
    function saveScrollPosition() 
    {
    alert(document.getElementById("theBody").scrollTop);
    if (document.documentElement) document.getElementById("__SCROLLPOS").value =document.documentElement.scrollTop; 
    else if (document.body) document.getElementById("__SCROLLPOS").value = document.body.scrollTop;
    }

    document.onscroll=saveScrollPosition();
    </script><script language='javascript'>
    function setScrollPosition() 

    document.documentElement.scrollTop = document.getElementById("__SCROLLPOS").value;
    }

    document.onload=setScrollPosition();
    </script>
    <br><br><br><br><br><br><br><br><br><br>
    </BODY>
    </HTML>
      

  2.   

    看看下面的代码<HEAD>
    <TITLE> New Document </TITLE>
    </HEAD><BODY id="theBody">
    <form action="#" method="post">
    <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
    <input type="text" name="__SCROLLPOS" id="__SCROLLPOS" value="10" onclick="javascript:alert(this.scrollLeft);">
    <input type="submit" value="submit">
    </form>
    <br>
    <script language='javascript'>
    function saveScrollPosition() 
    {

        document.getElementById("__SCROLLPOS").value = document.body.scrollTop;
    alert(document.getElementById("theBody").scrollTop);
    }

    document.body.onscroll=saveScrollPosition;
    function setScrollPosition() 

    document.body.scrollTop = document.getElementById("__SCROLLPOS").value;
    }

    document.onload=setScrollPosition;
    </script>
    <br><br><br><br><br><br><br><br><br><br>
    </BODY>
    </HTML>
    判断document.documentElement和document.body仿佛意义不大