JavaScript 函数破坏了我在 jQuery 地址爬行中的深层链接
JavaScript 与 jQuery Address(插件)的奇怪行为。
我有这段代码:
var handler = function(data) {
$('#conteudo').hide().html($('#conteudo', data).html()).fadeIn(500);
$.address.title(/>([^<]*)<\/title/.exec(data)[1]);
};
它有效。完美。
现在我更改代码:
var handler = function(data) {
$('#conteudo').fadeOut(500, function() {
$('#conteudo').html($('#conteudo', data).html()).fadeIn(500);
});
$.address.title(/>([^<]*)<\/title/.exec(data)[1]);
};
现在淡出效果有效,并且在淡入后(使用新内容)。美丽的!但是编写新内容的方式(在新函数内部,fadeOut 之后)的这个小变化破坏了我页面内的子链接。
这是一个实例:
- 访问此 URL: http://impulse.im/clean/2/
- 在顶部菜单中,单击“Contato”。
- 现在查看加载内容中链接“Rafa”的
href
!http://impulse.im/clean/2?_escaped_fragment_=%2Fcontato%2Frafa
。
这是不正确的:它应该是http://impulse.im/clean/2/#!/contato/rafa
- 再次:http://impulse.im/clean/2/ - 单击“Contato”。现在重新加载页面。
- 链接“Rafa”现在是正确的。
这个新函数(在 fadeOut
之后)对代码做了什么?为什么这个函数会破坏我的链接?
谢谢!
Strange behavior of JavaScript with jQuery Address (plugin).
I have this code:
var handler = function(data) {
$('#conteudo').hide().html($('#conteudo', data).html()).fadeIn(500);
$.address.title(/>([^<]*)<\/title/.exec(data)[1]);
};
And it works. Perfectly.
Now I change the code:
var handler = function(data) {
$('#conteudo').fadeOut(500, function() {
$('#conteudo').html($('#conteudo', data).html()).fadeIn(500);
});
$.address.title(/>([^<]*)<\/title/.exec(data)[1]);
};
Now the fade out effect works, and after fade in (with new content). Beautiful! But this little change in the way of writing the new content (inside of the new function, after fadeOut) broke my sub-links inside my pages.
Here's a live example:
- Access this URL: http://impulse.im/clean/2/
- In the top menu, click on 'Contato'.
- Now look the
href
of link 'Rafa' in the loaded content!http://impulse.im/clean/2?_escaped_fragment_=%2Fcontato%2Frafa
.
This is not correct: it should readhttp://impulse.im/clean/2/#!/contato/rafa
- Again: http://impulse.im/clean/2/ - Click on 'Contato'. Now RELOAD the page.
- The link 'Rafa' is now correct.
What this new function (after fadeOut
) is doing with the code? Why this function is breaking my link?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于,您在数据中存储的 html 实际放置在页面上之前调用了地址插件。发生这种情况是因为您异步调用
$('#conteudo').html($('#conteudo', data).html()).fadeIn(500)
因为它是作为回调调用的淡出方法。以这种方式更改:
这将在新内容放置在页面中后调用您的地址插件。
之前是这样工作的。
处理程序返回数据->内容淡出->您调用了地址插件,但内容尚未放置在页面上 -> 500 毫秒后,添加内容的回调被调用。
现在就会变成这样了。
处理程序返回数据->内容淡出-> 500ms 后添加内容并调用地址插件
The problem is that you are calling the address plugin before the html stored in data is actually placed on the page. It happens because you call the
$('#conteudo').html($('#conteudo', data).html()).fadeIn(500)
asynchronously as it's called as a callback to the fadeOut method.Change it this way:
This will call your address plugin after the new content was placed in the page.
Before it worked like this.
handler returns data -> content fades out -> you call the address plugin but the content isn't placed on the page yet -> after 500ms you the callback adding the content is called.
Now it'll be like this.
handler returns data -> content fades out -> after 500ms the content is added and the address plugin is called