两个具有相同函数名称的 jquery 插件之间的冲突 [英] conflict between two jquery plugins with same function name
问题描述
我在一个大型站点工作,该站点包含 2 个相互冲突的 jquery 插件,用于执行 autocmplete.
I am working in a large site that has 2 conflicting jquery plugins included for doing autocmplete.
1) jquery.autocomplete.js(不是 jquery ui 的一部分):
1) jquery.autocomplete.js (not part of jquery ui) that does :
$.fn.extend({
autocomplete: function ...
2) jquery.ui.autocomplete.js(来自最新的 jquery ui 库),也使用 autocomplete 关键字.
2) jquery.ui.autocomplete.js (from the latest jquery ui library), that also uses the autocomplete keyword.
$.widget( "ui.autocomplete", { ...
有没有办法指定我只使用第二个 jquery.ui 小部件打电话时
Is there a way to specify that i am using only the second, jquery.ui widget when calling
$( "#tags" ).autocomplete ...
不改变这两个文件?
推荐答案
由于第二个自动完成是使用 $.Widget
向 jQuery 注册自己的方法,因此最容易改变内部的.
As the second autocomplete is using the $.Widget
method of registering itself with jQuery it'll be easiest to change the behaviour of the in-house one.
如果不对两次脚本加载之间的 jQuery 对象进行某些更改,您将无法同时加载它们,因为它们只会相互冲突(或覆盖).
You won't be able to load both of them without making some sort of change to the jQuery object between the two script loads because they'll just conflict with (or overwrite) each other.
我会试试这个:
<script src="jquery.autocomplete.js"> </script>
<script>
// rename the local copy of $.fn.autocomplete
$.fn.ourautocomplete = $.fn.autocomplete;
delete $.fn.autocomplete;
</script>
<script src="jquery-ui.autocomplete.js"> </script>
然后会产生:
$().autocomplete()
使用 jQuery UI 版本,并且
use the jQuery UI version, and
$().ourautocomplete()
使用您的本地版本.
这篇关于两个具有相同函数名称的 jquery 插件之间的冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!