Dojo在URL中使用散列值来允许通过ajax调用加载页面的书签。 这是通过dojo.hash api完成的。 所以......我认为你能做的最好的事情就是用它来触发你在主页面中写的回调。
对于滚动到已加载内容中的给定位置,可以使用node.scrollIntoView()。
例如,假设你有一个名为“mainPane”的contentPane中加载名为“fragment.html”的HTML片段的页面,您的片段包含2个锚这样的:
-fragment.html :
<a href="#anchor1">Anchor 1</a>
<p>some very long contents...</p>
<a href="#anchor2">Anchor 2</a>
<p>some very long contents...</p>
现在说你的主页2个按钮(名为BTN1和BTN2),这将被用来加载片段,然后导航到适当的锚。然后,您可以连线,了下面的JavaScript,在你的主页:
<script type="text/javascript">
require(['dojo/on',
'dojo/hash',
'dojo/_base/connect',
'dijit/layout/BorderContainer',
'dijit/layout/ContentPane',
'dijit/form/Button'],
function(on, hash, connect){
dojo.ready(function(){
var contentPane = dijit.byId('mainPane');
var btn1 = dijit.byId('btn1');
var btn2 = dijit.byId('btn2');
btn1.on("Click", function(e){
if (!(contentPane.get('href') == 'fragment.html')) {
contentPane.set("href", "fragment.html");
}
hash("anchor1");
});
btn2.on("Click", function(e){
if (!(contentPane.get('href') == 'fragment.html')) {
contentPane.set("href", "fragment.html");
}
hash("anchor2");
});
// In case we have a hash in the URL on the first page load, load the fragment so we can navigate to the anchor.
hash() && contentPane.set("href", "fragment.html");
// This callback is what will perform the actual scroll to the anchor
var callback = function(){
var anchor = Array.pop(dojo.query('a[href="#' + hash() + '"]'));
anchor && anchor.scrollIntoView();
};
contentPane.on("DownloadEnd", function(e){
console.debug("fragment loaded");
// Call the callback the first time the fragment loads then subscribe to hashchange topic
callback();
connect.subscribe("/dojo/hashchange", null, callback);
});
}); // dojo.ready
}); // require
</script>
你的Javascript在你的html页面的底部? – 2011-12-31 15:48:19
它位于
和之内,但位于页面的底部。 – user1124421 2011-12-31 16:13:53这是否跨浏览器相同?任何输入的形式?其他可能会争取焦点的东西() – rene 2011-12-31 19:37:52