流程锚(散)与jQuery和Ajax链接 [英] Process anchor (hash) links with jQuery and Ajax
问题描述
假设我要定位到以下链接:
Suppose I want to navigate to the following link:
http://www.mysite.com/feature#linktosection
http://www.mysite.com/feature
加载使用jQuery阿贾克斯的内容,所以我不能浏览到 #linktosection
,直到ajax的内容加载。一旦它被加载,我需要导航(也许模拟点击),以 #linktosection
The content of http://www.mysite.com/feature
is loaded with jQuery ajax, so I cannot navigate to #linktosection
until the ajax content has been loaded. Once it is loaded I need to navigate (maybe simulate a click) to #linktosection
这是我的jQuery ajax调用:
This is my jQuery ajax call:
$('.changecontext-button').click(function()
{
$.ajax({
type: "POST",
url: $(this).attr('href'),
success: function(data) {
diffSection.html(data);
},
error: function(xhr, textStatus, errorThrown) {
diffSection.html(xhr.responseText);
}
});
});
你知道如何使用jQuery做到这一点?
Do you know how to do this with jQuery?
这是另一种可能是解析HREF链接,并把它分割成的基本URL 的和的主播网址的。该操作成功,我可以用jQuery选择,以获得锚链接和模拟。点击()。
An alternative could be parsing the href link and separate it into a base url and an anchor url. The, on success I could use a jQuery selector to get the anchor link and simulate a .click().
有没有其他更好的选择,使用jQuery或JavaScript?
Is there any other better alternative, using jQuery or JavaScript?
在先进的感谢。
推荐答案
最后,我实现它,如下所示:
Finally, I implemented it as follows:
$('.changecontext-button').click(function()
{
var targetUrl = $(this).attr('href');
$.ajax({
type: "POST",
url: targetUrl,
success: function(data) {
diffSection.html(data);
var anchor = getAnchor(targetUrl);
if (anchor)
{
$(anchor).click();
}
},
error: function(xhr, textStatus, errorThrown) {
diffSection.html(xhr.responseText);
}
});
});
...
function getAnchor(url)
{
var index = url.lastIndexOf('#');
if (index != -1)
return url.substring(index);
}
这篇关于流程锚(散)与jQuery和Ajax链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!