如何更改jQuery的命名空间? [英] How to change Namespace of jQuery?

查看:79
本文介绍了如何更改jQuery的命名空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个内部使用jQuery的库,该库运行良好.但是我不想因为用户在代码中使用我的库而强迫用户使用我的jQuery版本.

I'm working on a library which uses jQuery internally which work fine. But I don't want force the user to use my jQuery version just because he uses my library in his code.

问题是,如何使用其他名称的jQuery?

So the question is, how can I use jQuery under a different name?

做这样的事情是否足够:

Is it sufficient to do something like this:

var mylib.jQuery = {};  
mylib.jQuery = jQuery.noConflict(true);

这将使jQuery在mylib.jQuery下可用,并释放$符号,但是原始的jQuery符号仍然有效(我认为不太好).

This will make jQuery available under mylib.jQuery and free the $ symbol, but the original jQuery symbol still works (which I think is not so good).

推荐答案

您必须进入源代码,因为在其中定义了window.jQuery,并且只需更新引用名称即可.

You'd have to go into the source because window.jQuery is defined there, and just update the reference name.

这似乎是不合理的请求.

This seems like an unreasonable request though.

看起来您要做的就是将引用设置为$ .noConflict:

Looks like all you have to do is set the reference to $.noConflict:

<script src="jquery.js"></script>
<script>
var foo = {};
foo.jQuery = jQuery.noConflict( true );
alert( typeof jQuery );
alert( typeof $ );
alert( typeof foo.jQuery );
</script>

但是,您必须在包含脚本之后立即执行此操作.我以前的方法是直接在脚本中完成的:

You'd have to do this right after you include the script though. My previous way did it directly in the script:

http://173.45.226.115/jquery.html

这篇关于如何更改jQuery的命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆