如何使用jquery noConflict而不改变第三方库

How to use jquery noConflict without changing third party library

本文关键字:改变 第三方 何使用 jquery noConflict      更新时间:2023-09-26

我需要使用第三方提供的jquery编写的一些库。一旦我尝试合并jquery,我们最终会在代码库中与$ sign发生冲突。我能够通过为$ sign创建别名来解决冲突,然后在第三方库中使用别名更新$ sign。因为它是由第三方编写的代码,我不是很熟悉jquery,我宁愿找到一个不涉及更新第三方代码本身的解决方案。我也有一个内联javascript(也由第三方提供),需要在页面本身上被解雇。我该怎么做呢?下面是我使用的jquery文件的例子:

<!-- jquery libs -->
<script src="/js/jquery-1.11.1.min.js"></script>
<script src="/js/jquery-ui.min.js"></script>
<!-- third party library using jquery code-->
<script  type="text/javascript" src="/js/jqueryTest.js"></script>
<!-- inline javascript that gets fired on the page itself -->
 <script>
  $('#formvalue').jqueryTest({
   subdomain: 'xyz',
   selectData: function (data) {
   $('#formvalue').val(data.value);
   $('#form').submit();
  });
 </script>

这样你就可以写:

var jq = $.noConflict();
jq(document).ready(function(){
    jq("button").click(function(){
        jq("p").text("jQuery is still working!");
    });
}); 

更多信息请参考以下链接http://alexmarandon.com/articles/widget-jquery-plugins/

希望有帮助

这不是编辑第三方库的正确方法,因为它不可维护。

从https://api.jquery.com/jquery.noconflict/:

查看此代码
<script src="other_lib.js"></script>
<script src="jquery.js"></script>
<script>
    $.noConflict();
    // Code that uses other library's $ can follow here.
</script>

试试这个:

<!-- jquery libs -->
<script src="/js/jquery-1.11.1.min.js"></script>
<script src="/js/jquery-ui.min.js"></script>
<!-- third party library using jquery code-->
<script type="text/javascript" src="/js/jqueryTest.js">    </script>
<!-- inline javascript that gets fired on the page itself  <script>
$.noConflict();
$('#formvalue').jqueryTest({
    subdomain: 'xyz',
    selectData: function (data) {
        $('#formvalue').val(data.value);
        $('#form').submit();
    }
});
</script>