Facebook 连接、jQuery UI 和 jQuery.noConflict()

问题描述:

我正在尝试在我的个人网站上构建一个页面,该页面既使用了 jQuery,又实现了 Facebook Connect.

I'm trying to build a page on my personal website that both used jQuery and implements Facebook Connect.

不幸的是,Facebook 客户端 API 使用了 $ 令牌,这意味着我必须调用 jQuery.noConflict()

Unfortunately, the Facebook client API uses the $ token, which means I have to call jQuery.noConflict()

双重不幸,我发现出于某种疯狂的原因和 正如 Rick Strahl 指出的,jQuery UI 不尊重 noConlict().其实,如果你看源码,$就在上面.

Double-unfortunately, I've found out that for some crazy reason and as Rick Strahl points out, jQuery UI doesn't respect noConlict(). At all. In fact, if you look at the source code, there are $s all over it.

真的希望能够使用 jQuery UI - 具体来说,dialog() 组件和 draggable 会非常好,因为好吧 - 但我更重要的是,我不想手动编辑 - 并测试和维护 - 我自己的 jQuery UI 任何部分的副本.

I really want to be able to use jQuery UI - specifically, the dialog()component, and draggable would be really nice as well - but I even moreso, I don't want to have to hand-edit - and test, and maintain - my own copy of any part of jQuery UI.

这是一系列 我不得不刮胡子 这让我不知所措.有什么建议?帮助!

This is the most recent in a series of yaks I've had to shave which has me at my wits' end. Any suggestions? Help!

您引用的帖子已经很旧而且已经过时了.jQuery UI1.0 版本在几个文件中存在此问题,并在报告后立即修复.

The post you've referenced is quite old and out of date. The 1.0 release of jQuery UI had this issue in a couple of files and was fixed as soon as it was reported.

所有的 jQuery UI 都被包裹在一个 closure 中,它以 $ 的形式传入 jQuery,因此可以在内部使用 $ 而 $ 在外部用于其他东西.

All of jQuery UI is wrapped in a closure that passes in jQuery as $ and therefore can use $ internally while $ is used for something else externally.

来自 http://docs.jquery.com/Using_jQuery_with_Other_Libraries#Referencing_Magic_s_for_jQuerya>

From http://docs.jquery.com/Using_jQuery_with_Other_Libraries#Referencing_Magic_-_Shortcuts_for_jQuery

使用以下技术,它允许您在块内使用 $代码没有永久覆盖 $:

Use the following technique, which allows you to use $ inside of a block of code without permanently overwriting $:

(function($) {
  /* some code that uses $ */
})(jQuery)

注意:如果您使用这种技术,您仍然可以通过 window.$ 使用 Prototype,例如 window.$('some_element_id').任何引用 $ 的闭包外部的函数都会调用 Prototype,即使是从闭包内部调用也是如此.

Note: If you use this technique, you can still use Prototype via window.$ e.g., window.$('some_element_id'). Any function outside of your closure that references $ will invoke Prototype, even if called from inside your closure.

这就是为什么您会在 jQuery UI 文件中看到 $,但请放心,jQuery UI (1.5+) 的任何最新版本都完全支持 $代码>jQuery.noConflict()

This is why you'll see $ inside the jQuery UI files, but rest assured, any recent version of jQuery UI (1.5+) is completely supported with jQuery.noConflict()