jQuery插件使用相同的功能名称->冲突 [英] Jquery plugins occupy the same function name -> conflict
问题描述
使用jquery暴露插件和Masked Input插件时,我遇到了一个问题.
问题在于它们都占用$.mask
函数,这会导致冲突.但是我非常需要这两个插件一起工作.我会将其中的$.mask
重命名为...,例如$.msk
,但是在这种情况下,我将始终需要记住它,并且如果要升级到新版本,将再次重命名.
I've ran into a problem using jquery expose plugin alongside Masked Input plugin.
The problem is they both occupy $.mask
function which leads to conflict. But I vitally need this two plugins to work together. I would rename $.mask
in one of them to... let's say, $.msk
, but in this case I'll always need to remember it and if I want to upgrade to new version, I will rename again.
寻找更好的解决方案,以解决jquery插件之间的这种冲突.
Looking for better solution on how to cope with this kind of conflicts between jquery plugins.
推荐答案
我认为我同意mu的回答.一种可能是使用 jQuery.sub().
I think I agree with mu's answer. One addition might be to use jQuery.sub().
由于大多数插件在构造过程中都使用jQuery全局,因此您应该能够在加载其中一个插件之前为jQuery加上别名.然后,您可以随后将别名重新设置为您选择的任何内容.
Since most plugins use the jQuery global during construction, you should be able to alias jQuery before loading one of the plugins. Then you could re-alias afterwards to whatever you choose.
<script src="http://cdn.jquerytools.org/1.2.5/full/jquery.tools.min.js?foo"></script>
<script type="text/javascript">
var jQuery = $.sub();
</script>
<script src="jquery.maskedinput.js" type="text/javascript"></script>
<script type="text/javascript">
var $$$ = jQuery;
jQuery = $;
</script>
此方法的唯一可能优势是,它可能会将一个插件与另一个插件可能对jQuery方法所做的更改隔离开来.有点令人费解,但我能想到的是更改插件的唯一选择.
The only possible advantage to this approach is that it might isolate one plugin from the changes that another plugin might make to jQuery methods. It's slightly convoluted but the only alternative to altering the plugins, that I can think of.
这篇关于jQuery插件使用相同的功能名称->冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!