$(window).load(handler) 不会触发
我一直在使用 jQuery 开发 Asp.NET Web 应用程序来生成甜美、灵活且快速的 UI。我已经创建了骨架
<div id='title'>
<h3 class='color-black h3'>
Choose the career</h3>
<p>
</p>
<p>
</p>
<p>
</p>
</div>
<div id='tabbedContent' class='tabbed_content'>
<div id='tabs' class='tabs'>
<div class='moving_bg'>
</div>
</div>
<div id='contentID' class='slide_content'>
<div id='tabsliderID' class='tabslider'>
</div>
</div>
</div>
,现在在开始时我将使用 jQuery 附加一些动态创建的 DOM 元素。我通过从 Page_Load 方法 (ASP) 调用两个 js 函数来实现这一点,这将实现所有的魔力。然后我需要初始化几个脚本(tabbedContent 和 iNettuts)。 问题是这些脚本仅在创建整个页面(包括动态创建的内容)后才需要初始化,所以我不能使用 $(document).ready ,当我使用 $(window).load 有时它可以工作,有时它可以工作没有。
我已经在谷歌上搜索了至少两个星期,但仍然一无所获。我应该怎么办? jQuery 不应该处理这个问题吗?如果没有..有什么解决方法吗?
附:尝试让它在 IE7-8、Chrome 10 和 FF 6 中工作
,顺便说一句,一旦我实现了这一点,我将需要执行一些 Web 服务调用以从数据库获取数据。这会影响行为吗?我应该期待更多的头痛吗?
I´ve been working with on a Asp.NET web app using jQuery to generate a sweet , flexible and fast UI. I´ve created the skeleton
<div id='title'>
<h3 class='color-black h3'>
Choose the career</h3>
<p>
</p>
<p>
</p>
<p>
</p>
</div>
<div id='tabbedContent' class='tabbed_content'>
<div id='tabs' class='tabs'>
<div class='moving_bg'>
</div>
</div>
<div id='contentID' class='slide_content'>
<div id='tabsliderID' class='tabslider'>
</div>
</div>
</div>
Now onStart I will append some dynamically created DOM elements using jQuery. I do this by calling two js functions from the Page_Load method (ASP) that will do all the magic . Then I'll need to init a couple of scripts (tabbedContent and iNettuts).
The thing is that those scripts need to be initialized only after the whole page is created -including the dynamically created content- so i CANT use $(document).ready and when I use $(window).load sometimes it works and sometimes it doesn't.
I´ve been googling it for at least two weeks and still nothing. What should I do? Isn't jQuery supposed to handle this issues? and if not .. is there any workaround?
ps. Tryin' to make it work in IE7-8, Chrome 10 and FF 6
BTW, once I achieve this i´ll need to do some web services calls to get data from the db. Will this affect the behavior ? Should I expect more headaches?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
$(document).ready(handler)
是正确的形式。您也可以只执行$(handler)
.load
方法是在元素上完成的,实际上用于将 XHR 调用的 HTML 响应加载到元素中。$(document).ready(handler)
is the correct form. You can also just do$(handler)
The
.load
method is done on elements and is actually used to load the HTML response from an XHR call into an element.您实际上不应该在 Page_Load 处理程序中从 .Net 调用 javascript 函数(当然这是可能的)。
相反,请将所有 jQuery 功能放入
$(document).ready()
中。然后,一旦页面加载并准备好通过 JavaScript 接受动态操作,它将被执行。这对于 IE6 兼容性尤其重要,因为如果您在页面完成加载之前尝试更改 DOM,则会出现阻塞。因此,在您的情况下,您的 jQuery javascript 代码可能如下所示:
You shouldn't really be calling the javascript functions from .Net in the Page_Load handler (though of course this is possible).
Instead, put all your jQuery functionality inside
$(document).ready()
. Then it will be executed once the page loads and is ready to accept dynamic manipulation via JavaScript. This is especially important for IE6 compatibility, as it will choke if you try and alter the DOM before the page has finished loading.So, in your case, your jQuery javascript code might look like:
Page_Load
非常适合您的 .ready 处理程序。如果您需要真正的动态 DOM 事件,您应该使用.live()
你的事件绑定。Page_Load
will work fine for your .ready handler. If you need true dynamic DOM events you should use.live()
for your event binding.尝试使用
$(document).load
代替。Try
$(document).load
instead.或者尝试
$(document).ready()
OR try
$(document).ready()
instead在
$(document).ready()
函数中,您可以创建一个哈希,其中包含需要加载为键的动态对象及其就绪状态作为值。每个动态对象完成加载后,将其值设置为 true。然后,您可以轮询以查看何时所有值都为 true,并在发生这种情况时执行您需要的代码。$(window).load()
有时有效,有时无效,因为有时所有元素加载得足够快,以便在代码运行时做好准备。Inside your
$(document).ready()
function, you could create a hash with the dynamic objects that need to load as keys and their ready states as values. Once each dynamic object finishes loading, you set its value to true. You can then poll to see when all the values are true, and execute the code you need when that happens.$(window).load()
sometimes works and sometimes doesn't because sometimes it happens for all the elements to load fast enough for them to be ready when your code runs.在对您的建议进行了一些研究以及实施 Radus 建议的一个非常非常漫长的夜晚之后(这可以解释为要加载的每个元素的标志),我开始问自己:如果 ready() 有一个标志怎么办?在这种情况下,我不应该担心初始化的时间。好吧,事实证明这个标志确实存在,它的名字叫holdReady()。
基本上,它会阻止启动 read() 事件,直到该标志设置为 false。
“$.holdReady() 方法允许调用者延迟 jQuery 的就绪事件。此高级功能通常由动态脚本加载器使用,这些加载器希望在允许就绪事件发生之前加载其他 JavaScript(例如 jQuery 插件),即使 DOM必须在文档的早期调用此方法,例如在 jQuery 脚本标记之后立即调用此方法,在就绪事件已经触发后将不会产生任何效果。”[1]
所以,我这样做了:
并且就是这样,问题解决了。希望这对其他人有帮助,谢谢大家。
[1] http://api.jquery.com/jQuery.holdReady/
after a bit of research on your suggestions and a very very long night implementing Radus suggestion -which could be interpreted as flags for each element to be loaded- I started to ask myself : what if ready() has a flag? in that case I shouldn't worry about the timing of the initialization. Well, turns out that this flag ,indeed, exists and it's called holdReady().
Basically, it prevents the ready() event to be launched until the flag is set to false.
"The $.holdReady() method allows the caller to delay jQuery's ready event. This advanced feature would typically be used by dynamic script loaders that want to load additional JavaScript such as jQuery plugins before allowing the ready event to occur, even though the DOM may be ready. This method must be called early in the document, such as in the immediately after the jQuery script tag. Calling this method after the ready event has already fired will have no effect."[1]
So, I did:
And that's it, problem solved. Hope this helps someone else, and thank you all.
[1] http://api.jquery.com/jQuery.holdReady/