如何模仿 jQuery.live(),或在存在原型库的情况下使用 2 个 jQuery 版本?
我们在客户端提供的 HTML“包装器”内呈现我们的内容。该 HTML 包装器包含对 jQuery 1.2.6 的引用,因此到目前为止我一直在使用 jQuery 功能。
我面临的问题是我正在动态呈现需要与其关联的单击事件的内容。单击事件与 $(document).ready() 上的元素关联。因此,我需要 1.3+ 版本中提供的 .live() 函数的功能。
所以我想知道我的选择是什么?
有什么方法可以让我轻松模仿 .live() 的功能,这样我就不需要 jQuery 库提供的函数了?
我需要在我们的内容中包含新的 jQuery 库吗?这并不理想,因为它会导致需要管理的冲突,并且我们已经在管理与原型库的冲突,其他人已将其包含在包装器中,并使用以下行:
jQuery(document).ready(function ($) {
除非有人可以向我展示一种简单的方法来做到这一点?
...或者我是否将其返回给客户,让他们升级他们的 jQuery?我不知道这种情况是否有可能发生。
谁能建议解决这个问题?谢谢
We are rendering our content inside a HTML 'wrapper' which is supplied by the client. That HTML wrapper contains a reference to jQuery 1.2.6 so that's what I've been using for my jQuery functionality up until now.
The problem I'm facing is that I'm dynamically rendering content that needs a click event associated with it. The click event is associated with the elements on $(document).ready(). As such, I need functionality of the .live() function which is available in version 1.3+.
So I'm wondering what are my options?
Is there any way for me to easily mimic the functionality of .live() so that I don't need the function supplied by the jQuery library?
Do I need to include the new jQuery library with our content? This is not ideal because it causes conflicts which need to be managed, and we're already managing conflicts with a Prototype library which somebody else has included in the wrapper with the following line:
jQuery(document).ready(function ($) {
unless somebody can show me an easy way of doing this?
... or do I put it back to the client that it's time for them to upgrade their jQuery? I don't know if it's likely that this will happen.
Can anyone suggest a solution to this problem? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以简单地滚动您自己的事件委托。例如,不要绑定到动态创建的元素,例如:
您可以将处理程序附加到更高级别的元素(例如文档),并过滤点击事件:
这是live的基本思想(以及较新的委托)继续运行。
*edited example to check the event target's parent elements as well
You could simply roll your own event delegation. For example, rather than binding to the dynamically created elements, e.g.:
You can instead attach a handler to a higher level element, like the document, and filter the click events:
This is the basic idea that live (and the newer delegate) operate on.
*edited example to check the event target's parent elements as well
你可以使用 livequery 插件吗?应该适用于该版本。
http://plugins.jquery.com/project/livequery/
Can you use the livequery plugin? Should work with that version.
http://plugins.jquery.com/project/livequery/