防止页面未加载时运行 JS
我遇到了一个问题。 当我使用 jQuery 加载包含大量 JavaScript 的页面时,页面会冻结。 我相信这是因为 js 在页面加载之前执行,因为我的本地站点没有冻结。 但是,$(document).ready(function(){}); 似乎不适用于动态加载的页面? 真的吗? 或我能做的任何事情来解决这个问题。 太感谢了!
I have encountered a problem. When I use jQuery to load a page that contains heavy javascript, the page freezes. I believe it is because the js executes before the page loads as my local site does not freeze. However, $(document).ready(function(){}); seems not working with dynamically loaded pages? is that true? or anything i could do to solve this problem. Thanks a million!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
它会在整个页面加载后运行js。
避免使用
它会在 DOM 加载后立即运行 js。
Try using
It will run the js after the whole page is loaded.
Avoid using
It will run the js just after the loading of DOM.
$(document).ready()
在动态页面中工作正常。 您的代码中一定有某个地方有错误。首先要做的就是尝试查看源代码并将 HTML 保存到纯
.html
文件中,然后在浏览器中加载该文件。 如果仍然失败,那么您就知道问题与服务器端 ASP/PHP/任何代码无关。 然后尝试逐步删除 HTML 和 JavaScript,直到问题消失。 这将帮助您缩小罪魁祸首的范围。 如果您可以将页面缩小到仍然显示问题的小文件,请将其发布到此处,我们将尽力提供帮助。$(document).ready()
works fine in dynamic pages. There must be an error in your code somewhere.The first thing to do is to try View Source and save the HTML to a plain
.html
file, then load that file in your browser. If that still fails then you know the problem has nothing to do with the server-side ASP/PHP/whatever code. Then try removing HTML and JavaScript piece by piece until the problem disappears. That'll help you narrow down the culprit line(s). If you can reduce your page to a small file that still demonstrates the problem, post that here and we'll try to help.