功能相同名称的两个jQuery插件之间的冲突 [英] conflict between two jquery plugins with same function name

查看:269
本文介绍了功能相同名称的两个jQuery插件之间的冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作中有包括做autocmplete 2冲突的jQuery插件大型网站。

I am working in a large site that has 2 conflicting jquery plugins included for doing autocmplete.

1)jquery.autocomplete.js(不jQuery用户界面的一部分),做:

1) jquery.autocomplete.js (not part of jquery ui) that does :

$.fn.extend({
    autocomplete: function   ...

2)jquery.ui.autocomplete.js(从最新的jQuery UI的库),也使用自动完成的关键字。

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 ...

在不改变两个文件?

without changing the 2 files?

推荐答案

由于第二自动完成使用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屋!

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