2012-06-17 44 views
2

我可以发送请求加载某个div与url只有像abc.com/#header1导致我使用的方法我必须为我的任务做5个单独的文件,并更新所有他们。 如果可以通过在我的一个文件中包含任何一段代码来完成,那么这对我来说是最好的。滚动到某个div与url

我想div上的滚动与url请求类似abc.com/#def其中def是页面应该加载的div的id。

+0

或类似abc.com?div=header1的值,并通过html页面获取值并相应地执行功能 –

+2

http://example.org/#header1默认情况下滚动到header1锚点。 –

回答

4

关于你previous question,这是你如何能做到这:

$(function(){ 
    // get hash value 
    var hash = window.location.hash; 
    // now scroll to element with that id 
    $('html, body').animate({ scrollTop: $(hash).offset().top }); 
}); 

你需要把这些代码在你想要元素滚动页面。因此,如果您访问yourdomain.com/#foo,它将滚动到id设置为foo的元素。

+0

我可以为你的功能添加时间延迟吗? –

+0

@softgenic:是的,你可以,有'animate'函数的参数,请参阅它的文档。 – Sarfraz

+0

是啊我bhai我做到了非常感谢..你能否也请检查这个问题,我已经找到了解决方案,但我心中有一些疑虑,它会工作与否。请检查我的问题中提到的解决方案是否正确http://stackoverflow.com/questions/10873034/transactions-and-php –

2

你可以使用jQuery在页面内加载特定的div。

Here is an example with fraction of code :  
$('#result').load('test.html', function() { 
    alert('Hello,I am in the div'); 
}); 

如果您需要显示特定网址的数据,那么您可以使用Ajax和Jquery。

Here is a small code for it: 
    $(document).ready(function(){ 
    $("#result").load(function(){ 

      $.ajax({ 
      type: 'GET', 
      url: "http://localhost/a.html", 
      success:function(data){ 
      alert(data); 
      } 
     }); 
    return false; 
    }); 
}); 

希望这可以帮助你。

0

试试这个:document.getElementById('elmID')。scrollIntoView(true);