jQuery ready事件没有在部分页面加载时触发 [英] jQuery ready event not fired on partial page load

查看:148
本文介绍了jQuery ready事件没有在部分页面加载时触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是原地:一个页面,其中包含html并使用jQuery libray和jQuery UI插件加载另一个页面,当某个选项卡被点击。问题是当页面/ html被加载/渲染(让我们简化这个,并说它只是做一些像$(#myDiv)。load(url);),ready事件没有被解雇,因为当然是窗口已经加载并触发了加载事件。这意味着在页面的加载(局部加载)上,我不想执行任何jQuery事情都将被执行。 UI.tabs插件旨在将页面加载到其他选项卡,我们可以假设其他页面可能包含自己的jQuery ...所以应该有一些方法这个..



我可以想到非常可怕的方法来解决问题,比如在页面底部渲染一个脚本块(加载到一个div),这样做在完成所有的事情之后,就可以做好准备就绪假设浏览器已经渲染了页面,如果脚本块被击中)。然而,这是非常糟糕的做法。任何建议?

解决方案

您如何向服务器发出AJAX请求?如果你使用ASP.NET AJAX,那么Brian Hasden的答案就是你正在寻找的东西。如果您使用jQuery发送请求,那么您可以使用加载函数设置回调。

  $( .ajaxLink)。load(url,null,function(){
// code here
});

load()文档



或设置一个全局ajaxComplete事件,每次ajax调用完成时都会触发。

  $(document).ajaxComplete(function(){
// code here
});

ajaxComplete()文档


Here's the situ: A page which contains html and using the jQuery libray and tabs jQuery UI plugin loads another page when some tab is clicked. The problem is that when the page/html is loaded/rendered (let's simplify this and say it's just doing something like $("#myDiv").load(url);), the ready event is not fired because of course the "window" has already loaded and fired the load event. This means that none of the jQuery things I want to do on load (partial load) of the page are executed. The UI.tabs plugin is designed to load pages into other tabs and we can assume that other pages may contain their own jQuery... so there should be some way around this..

I can think of very horrible ways to get over the problem, like have a script block at the bottom of the page being rendered (loaded into a div) which does all things I would do when ready is fired (as you can assume the browser has rendered the page already if the script block is hit). This however is VERY bad practise. Any suggestions?

解决方案

How are you firing the AJAX request to the server? If you're using ASP.NET AJAX, then Brian Hasden's answer is what you are looking for. If you are using jQuery to send the request, then you can either set a call back using the load function.

$(".ajaxLink").load(url, null, function() {
    // code here
});

load() Documentation

or set a global ajaxComplete event that is fired every time an ajax call is complete.

$(document).ajaxComplete(function() {
    // code here
});

ajaxComplete() Documentation

这篇关于jQuery ready事件没有在部分页面加载时触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆