简体   繁体   English

在自定义库中包含JQuery。 如何解决冲突问题。

[英]Include JQuery inside a custom library . How to resolve conflict issues.?

I am developing a new JS library. 我正在开发一个新的JS库。 I want to include JQuery as core part of it. 我想将JQuery作为其核心部分。

My library is supposed to work on all kind of websites, so in those sites JQuery may be already included with different version , may have some other library which may conflict with JQuery inside my library. 我的库应该适用于所有类型的网站,因此在这些网站中JQuery可能已经包含在不同的版本中,可能还有一些其他库可能与我的库中的JQuery冲突。

jQuery.noConflict();  

may not be a complete solution , since it will remove variables from the global scope. 可能不是一个完整的解决方案,因为它将从全局范围中删除变量。 My library don't want to change any other settings by user. 我的图书馆不希望用户更改任何其他设置。

So how can i avoid these issues 那我怎么能避免这些问题呢

  • Conflict with other library ? 与其他图书馆冲突? ( $ alias - may be jQuery.noConflict(); ). $ alias - 可能是jQuery.noConflict(); )。
  • Conflict with other versions of JQuery , if it is already included in the web page. 如果它已经包含在网页中,则与其他版本的JQuery冲突。

Thanks in advance. 提前致谢。 Please don't tell that not include JQuery , use normal JS :) 请不要告诉不要包含JQuery,使用正常的JS :)

It's very simple. 这很简单。

First load your preferred jQuery, and get a handle to that by omitting any conflicts from already existing jQuery by using, 首先加载你喜欢的jQuery,并通过使用省略现有jQuery的任何冲突来获取它的句柄,

    $yourJQ = window.jQuery.noConflict(true);

Use $yourJQ in a anonymous scope in all the functions that you want use your jQuery. 在要使用jQuery的所有函数中,在匿名作用域中使用$ yourJQ。 Like this, 像这样,

   (function($){
     // external library code   

    })($yourJQ);

Hope this helps 希望这可以帮助

Also visit http://alexmarandon.com/articles/web_widget_jquery/ 另请访问http://alexmarandon.com/articles/web_widget_jquery/

Well regardless of whether you remove variables from the global scope or not you will have to do a find & replace to change which library points at which version of jQuery; 那么无论你是否从全局范围中删除变量,你都必须进行查找和替换以更改哪个库指向哪个版本的jQuery; because they will either use $ or jQuery & conflict in the global scope, anyway. 因为他们要么在全球范围内使用$或jQuery和冲突,无论如何。 You could try something like this: 你可以尝试这样的事情:

Load first version of jQuery 加载第一个版本的jQuery

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.js"></script>
<script type="text/javascript">
  jQuery.noConflict();
  (function(window,$) {
    window.jQuery_v1 = $;
  })(window,jQuery);
  delete jQuery;
</script>

Load second version of jQuery 加载第二版jQuery

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>
<script type="text/javascript">
  jQuery.noConflict();
  (function(window,$) {
    window.jQuery_v2 = $;
  })(window,jQuery);
  delete jQuery;
</script>

Use both versions of jQuery 使用两个版本的jQuery

<script type="text/javascript">
  // this is now one version of jQuery
  console.log(jQuery_v1);
  // this is now another version of jQuery
  console.log(jQuery_v2);
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM