我在我正在处理的网页上按需加载内容。由于各种原因,我还使用滚动到:target片段的默认浏览器行为。您知道,您点击的链接看起来像#abc
,浏览器会滚动到您网页中的id="abc"
。
除非内容异步可用,否则不完全是这样。根据我的test page,您必须单击链接两次才能让浏览器滚动到异步添加的元素或CSS3 :target
选择器选择它。那么我可以做些什么来让我的用户不必点击两次?
答案 0 :(得分:1)
向.load
添加一个回调函数,该函数滚动到刚刚添加的元素。
$(document).ready(function () {
"use strict";
$("a[href=#loadme]").click(function () {
if ("testcomplete" in window && window.testcomplete) {
return;
}
$("#loadhere").load("loadme.inc", function(){
location.href = this.href;
//Your method to scroll "for various reasons"
});
window.testcomplete = true;
return true; //Prevent default behaviour, ie following the `href` target
})
})