使用url滚动到某个div
我可以发送请求加载某个div与URL只有类似abc.com/#header1的原因导致我使用的方法我必须为我的任务制作5个单独的文件并更新所有这些。 如果可以通过在我的一个文件中包含任何代码来完成,那么它对我来说是最好的。
我希望div上的滚动与url请求类似于abc.com/#def,其中def是页面应该加载的div的id。
关于您之前的问题 ,您可以这样做:
$(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
的元素。
您可以使用jQuery在页面中的特定div中加载一些URL。
Here is an example with fraction of code : $('#result').load('test.html', function() { alert('Hello,I am in the div'); });
如果您需要显示该特定URL的数据。那么,您可以使用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; }); });
希望这对你有所帮助。
试试这个:document.getElementById(’elmID’)。scrollIntoView(true);