首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在通过jQuery.load()加载外部元素之后,可以滚动到锚点吗?

在通过jQuery.load()加载外部元素之后,可以滚动到锚点吗?
EN

Stack Overflow用户
提问于 2015-10-13 19:52:18
回答 2查看 1.3K关注 0票数 2

设想的情况是:

  • 从RSS提要链接到我的网站http://website.com/#anchorelement
  • 我想跳转/滚动到#anchorelement

但是,在#anchorelement http://website.com/准备就绪后,将使用jQuery.load() 加载。因此,使用链接http://website.com/#anchorelement不会跳转到#anchorelement,因为在执行跳转时元素尚未加载。

有人对如何使这项工作有任何建议吗?是否可以使用javascript拦截锚跳转,并使其等待jQuery.load()调用完成?

谢谢!

EN

Stack Overflow用户

回答已采纳

发布于 2015-10-13 20:10:31

代码语言:javascript
运行
复制
$(function(){
  // so this is inside your document ready function.
  // you need to add a callback
  $( somecontainer ).load('somecontent', function(){
    // after the content has been loaded
    // check if a hash is present in the location
    if( window.location.hash != '' ){ 
      // find an element with matching id
      var anchor = $( location.hash ).get(0);
      // if element was found scroll it into view
      if( anchor ){ anchor.scrollIntoView() }
    }
  });
});
票数 2
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33111426

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档