gpt4 book ai didi

javascript - 通过jQuery.load()加载外部元素后是否可以滚动到 anchor ?

转载 作者:行者123 更新时间:2023-11-27 23:37:36 25 4
gpt4 key购买 nike

场景是这样的:

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

然而,#anchorelement使用 jQuery.load() 加载之后 http://website.com/准备好了。结果,使用链接 http://website.com/#anchorelement不会跳转到 #anchorelement因为执行跳转时元素尚未加载。

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

谢谢!

最佳答案

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

关于javascript - 通过jQuery.load()加载外部元素后是否可以滚动到 anchor ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33111426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com