2012-06-17 43 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);