如何从jquery ajax句柄页面使用jquery scrolltop?

时间:2011-05-18 20:07:22

标签: jquery ajax scrollto

如何从jquery ajax句柄页面使用jquery scrolltop?

页面aa.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery.js"></script> 
<script type="text/javascript" src="jquery.scrollTo-min.js"></script>        
<script type="text/javascript">     
    jQuery(document).ready(function(){
         $.ajax({
             url: "bb.php", 
             dataType: "html",
             type: 'POST', 
             data: "word=hello", 
             success: function(data){ 
                $("#result").html(data);
             }
          });
        });
</script>
<script type="text/javascript">
   jQuery(document).ready(function(){
       $(".scrolltoanchor").click(function() {
        $.scrollTo($($(this).attr("href")), {
            duration: 750
        });
        return false;
    });
   });
</script>
</head>
<body id="body">
<div style="padding-top:600px;">jquery scrollto test</div>  
<div id="result" style="padding-top:600px;"></div>
<div style="padding-top:600px;">
  <a class="scrolltoanchor" href="#body">back to top</a><!-- this can scroll to the top via jquery.scrollTo function -->
</div>
</body>
</html>

页面bb.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   
</head>
<body>
<a class="scrolltoanchor" href="#body"><?php echo $_POST['word']; ?>: back to the top</a>!-- this can't scroll to the top via jquery.scrollTo function, just run aa.html?#body as a html anchor -->
</body>
</html>

那么,如何在bb.php中添加scrolltop以便它可以使用动画滚动到aa.html的顶部?谢谢。

1 个答案:

答案 0 :(得分:1)

您应该将它放在AJAX成功函数中。

编辑:您也不需要在ajax页面中使用doctype,head等。