不错的 hijax 偶尔会失败
我使用以下 jquery 进行 hijaxing。它非常简洁,因为它假设大多数链接“想要”劫持,但让那些不通过“nohijax”类“选择退出”的链接。 为什么偶尔会失败?
$("#content a:not(.nohijax), #footer a:not(.nohijax)").live("click", function () {
$.get($(this).attr("href"), function (response) {
$("#content").replaceWith($("#content", response));
});
return false;
});
在我的机器上,它在 Chrome 中 100% 运行,在 IE9 中 100% 运行,但在其他机器上的 IE 中很少失败。失败的情况并不一致。即使在“故障”机器上,劫持在大多数情况下都可以工作,但突然单击的链接将显得“什么也不做”,因为旧内容没有被新内容替换。 “在新选项卡中打开”始终有效,表明新内容确实已发送。
我试图在这里研究这个问题。将 .not() 与 .live() 一起使用时必须小心,但我相信我有这一权利。此外,被替换的元素(此处为 id="content" 的 div)不得是 body 元素的直接子元素。事实并非如此(中间还有另一个 div)。
如果没有明显的嚎叫声,我如何追踪发生了什么? (我是 javascript 和浏览器开发的新手)。谢谢。
I use the following jquery for hijaxing. It's quite neat as it assumes the majority of links "want" hijaxing but lets those that don't "opt out" by having the class "nohijax". Why does it occasionally fail?
$("#content a:not(.nohijax), #footer a:not(.nohijax)").live("click", function () {
$.get($(this).attr("href"), function (response) {
$("#content").replaceWith($("#content", response));
});
return false;
});
It works 100% in Chrome, 100% in IE9 on my machine, but fails infrequently in IE on other machines. The failures are not consistent. Even on a "failing" machine the hijaxing works most of the time but suddenly a clicked link will appear to "do nothing" because the old content is not replaced with the new. "Open in new tab" always works, showing that the new content is indeed sent.
I've tried to research the problem here. One has to be careful using .not() with .live() but I believe I have that right. Also the replaced element (here, a div with id="content") must not be a direct child of the body element. It isn't (there's another div in between).
If there's no obvious howler, how can I trace what's going on? (I am new to javascript and browser development). Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 console.log(); 很好。找出您要返回的数据。例如,console.log(response) 将在浏览器的开发工具控制台中显示数据。
前任。
至于.live功能不起作用,我认为IE 8及以下版本该功能可能存在问题。您可能最好使用 .click() ,除非要使用的 html 锚点是在页面加载后填充的。
如果您使用最新的 jquery 1.7,则 .live() 方法已被弃用,但如果您不使用 1.7,建议您使用 .delegate() 代替。
http://api.jquery.com/live/
Its good to use console.log(); to find out what data you are returning. So for example, console.log(response) will display the data in the dev tools console of your browser.
ex.
As for the .live function not working, I think there might be problems that IE 8 and below have with that function. You might be better off using .click(), unless the html anchors that is to be used is populated after page load.
If you are using the latest jquery 1.7 the .live() method is deprecated, but if you are not using 1.7 its recommended that you use .delegate() instead.
http://api.jquery.com/live/
这似乎是一个缓存问题,我已经使用这些技术之一解决了。
我使用console.log,发现新内容与旧内容相同。清除浏览器缓存后,劫持起作用了。话虽如此,它在 Chrome 中从未出错,而且我看不出 IE 如何认为缓存的内容与请求的 URL 匹配。无论如何,现在它可以工作了。
如果它有用,这里是我的脚本更新为使用 jquery 1.7 和 .on():
This seems to have been a cache issue which I have solved with one of these techniques.
I used console.log and found that the fresh content was identical to the old. After clearing the browser cache the hijaxing worked. Having said that, it never went wrong in Chrome and I cannot see how IE thought the cached content was a match for the requested URL. It works now anyway.
In case it's useful, here is my script updated to use jquery 1.7 and .on():