javascript:如何在没有jquery的情况下编写$(document).ready之类的事件
在 jquery $(document).ready(function) 或 $(function) 中,如果没有 jquery,我怎么能做同样的事情,并且我需要浏览器兼容,并允许附加多个函数。
注意:dom准备好!=窗口onload
in jquery $(document).ready(function) or $(function) , how could I do the same thing without jquery, and I need browser compatiable, and allow to attach more than one function.
Note: dom ready!= window onload
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这就是 jQuery 包装您正在寻找的函数的方式 - 该代码片段不需要 jQuery,并且是跨浏览器兼容的。我已将所有对 jQuery.ready() 的调用替换为
yourcallback
- 您需要定义它。这里发生了什么:
DOMContentLoaded
,该函数将在 DOMContentLoaded 事件触发时使用 - 它确保回调仅被调用一次。document.addEventListener
/document.attachEvent
)并将回调绑定到它(IE 和普通浏览器不同,加上 onload 回调)从 jQuery 1.4.3 开始,函数 bindReady() 和 DOMContentLoaded :
这是 51 行纯 JavaScript 代码,只是为了可靠地注册事件。据我所知,没有更简单的方法。去展示像 jQuery 这样的包装器的优点:它们包装了功能嗅探和丑陋的兼容性问题,以便您可以专注于其他事情。
This is the way jQuery wraps the functions you're looking for - the snippet does not need jQuery, and is cross-browser compatible. I've replaced all calls to jQuery.ready() with
yourcallback
- which you need to define.What goes on in here:
DOMContentLoaded
is defined, which will be used when the DOMContentLoaded event fires - it ensures that the callback is only called once.document.addEventListener
/document.attachEvent
) and bind the callbacks to it (different for IE and normal browsers, plus the onload callback)Lifted from jQuery 1.4.3, functions bindReady() and DOMContentLoaded:
That's 51 lines of pure JavaScript code, just to register the event reliably. As far as I know, there is no easier method. Goes to show what the wrappers like jQuery are good for: they wrap the capability sniffing and ugly compatibility issues so that you can focus on something else.
有史以来最小的 DOMReady 代码。
Smallest DOMReady code, ever.
如果您支持 IE9+ 和现代(2013)版本的 Chrome、FF、Safari 等,这就是您所需要的。
This is all you need if you're supporting IE9+ and modern (2013) versions of Chrome, FF, Safari, etc.
这是我使用的一种方法,它似乎工作可靠
非常简单,它只是不断尝试将一个空的
元素附加到
document.body
中。如果文档未“准备好”,则会抛出异常,在这种情况下,它将使用新的setTimeout
调用再次尝试。一旦没有抛出异常,就会调用回调函数。我很高兴听到此方法是否有任何问题。它对我来说效果很好,但我还没有进行任何流行的 Javascript 框架都应该进行的广泛测试。
Here's a method I use that seems to work reliably
Pretty simple, it just keeps trying to append an empty
<span>
element todocument.body
. If the document is not "ready" an exception will be thrown, in which case it tries again with a newsetTimeout
call. Once no exception is thrown, it calls the callback function.I'd be happy to hear if there are any problems with this method. It has worked well for me, but I have not done the extensive testing that would be natural to any popular Javascript framework.
我见过很多不同的方法来尝试做到这一点。最简单的方法(我认为最初由雅虎建议)是在关闭正文标记之后调用初始化函数,有点唐突,但它是一行。
I've seen lots of different ways of trying to do this. The simplest way (suggested by yahoo initially, I think) is to just call your initializer function after the close body tag, a bit obtrusive, but it's a single line.
编辑
DomReady 事件在 JavaScript 中本身不存在。您可以通过关注 Dean Edwards 此处 和此处 有了这些,您就可以执行类似的事件附件在文档而不是窗口上进行处理。
查看 user83421 对 How我是否需要在 Javascript 中添加一个额外的 window.onload 事件
在这里也回顾一下。
Edit
The DomReady event does not exist nativly in javascript. You can implement your own by following some wonderful work done by people like Dean Edwards here and here with those in place you can perform a similar event attachment process on document instead of window.
Check out user83421's answer to How do I add an additional window.onload event in Javascript
To recap here as well.
我在“close body”标签之后和“close html”标签之前有这个。而且效果很好。加载预设功能将宽度、高度和位置值分配给 css div 标签。对于不同的屏幕尺寸很有用。
}
I have this after the 'close body' tag and before the 'close html' tag. and it works pretty well. The load presets function assigns width,height and position values to css div tags. useful for different screen sizes.
}