2010-05-05 52 views
0

当页面的URL中传递了该模式的ID时,我需要能够在页面上打开ID'd jQuery UI模式。例如:当通过URL打开jQuery Modal时,停止滚动到ID

http://mysite.com/visit.html#directions 

#directions是位于页面最底部的ID'd DIV,位于关闭BODY元素之前。它在页面加载时被实例化为一个jQuery UI对话框,然后我运行一个函数来检查当前的模式ID的URL。如果URL包含模式的ID,则打开模式。

这里是我的自动打开的功能是什么样子:

function autoOpenModal() { 
    if (document.location.href.indexOf('#')) { 
     var uriParts = document.location.href.split('#'); 
     var hashID = '#' + uriParts[1]; 
     if ($(hashID) && $(hashID).hasClass('dialog')) { 
      $(hashID).dialog('open'); 
     } 
    } 
} 

我遇到的问题是,浏览器在页面最底部的模式之前,自动向下滚动到ID被打开。所以当用户关闭模式时,他们在页面的底部(这对我来说似乎“破碎”)。

谁能告诉我如何停止浏览器自动滚动到ID?我看过event.preventDefault()但没有成功;我不确定在事件链中使用它的位置或方式。

回答

0

尝试返回从autoOpenModal()函数中错误:

function autoOpenModal() { 
    if (document.location.href.indexOf('#')) { 
     var uriParts = document.location.href.split('#'); 
     var hashID = '#' + uriParts[1]; 
     if ($(hashID) && $(hashID).hasClass('dialog')) { 
      $(hashID).dialog('open'); 
     } 
    } 
    return false; 
} 
+0

谢谢你的建议,karim。但我已经尝试过了,它似乎没有工作。我认为这是因为在执行document.ready()事件之前,浏览器在标记中向下跳转。 – markedup 2010-05-07 10:03:56

2

如果这是你的URL“http://mysite.com/visit.html#directions”网络浏览器应该总是跳转到“方向”因为这就是它所要求的。而不是使用“#”,也许你可以使用查询字符串。

http://en.wikipedia.org/wiki/Query_string