情况是这样的:
从RSS源链接到我的网站http://website.com/#anchorelement
我想跳转/滚动到#anchorelement
但是,#anchorelement在准备好后使用jQuery.load()加载。因此,使用链接http://website.com/不会跳转到http://website.com/#anchorelement,因为在执行跳转时元素尚未加载。
有人对如何使这个工作有什么建议吗?是否可以用javascript拦截锚跳跃,并让它等到#anchorelement调用完成?
谢谢!

最佳答案

$(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() }
    }
  });
});

10-06 00:15