簡體   English   中英

如何在不更改第三方庫的情況下使用jquery noConflict

[英]How to use jquery noConflict without changing third party library

我需要使用第三方提供的用jquery編寫的庫。一旦嘗試合並jquery,我們最終在代碼庫中與$符號發生沖突。 我能夠通過為$符號創建別名來解決沖突,然后在第三方庫中也使用別名更新$符號。 由於它是由第三方編寫的代碼,而且我對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>

(您在代碼中錯過了})。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM