簡體   English   中英

如何在給定的示例中使用jquery noConflict

[英]how to use jquery noConflict for the given example

我是新來使用jQuery的。 我需要使用我們代碼庫中第三方發送的jquery js文件。 不幸的是,它與已經使用的庫沖突。 我嘗試使用noConflict代碼,但是js文件中的功能無法識別相同的代碼。 有人可以幫我在第三方提供的js文件中使用noConflict嗎? 下面是示例:

    <script language="javascript" src="jquery-1.10.1.min.js"></script>
    <script language="javascript" src="thirdparty.class.js"></script> //this is third party js that uses jquery as well

    <script language="javascript">
      $(document).ready(function() {
        var test = new Test({ 
          baseUrl: 'http://example.com', 
          interval: 15000,
          testId: 8,

        });
      });
    </script>

below is the test.class.js sample file

function Test(parameters)
{
  if (parameters == undefined)
  {
    console.log("parameters must be defined");
    return;
  }

  if (parameters.baseUrl == undefined)
  {
    console.log("base url must be defined");
    return;
  }

  //complete code is not posted
}

無論如何,我可以避免在我的代碼和第三方js中使用jquery“ $”嗎?

任何幫助表示贊賞。 TIA

嘗試以下代碼,

jQuery(function($) {
    $(document).ready(function() {
        var test = new Test({ 
          baseUrl: 'http://example.com', 
          interval: 15000,
          testId: 8,

        });
    });
});

而且您也可以始終使用jQuery而不是$

jquery.min.js沖突

使用noConflict()方法為jQuery變量指定一個新名稱:

var test=$.noConflict(); 

 test(document).ready(function() {
    var test = new Test({ 
      baseUrl: 'http://example.com', 
      interval: 15000,
      testId: 8,

    });
  });

如果您有使用$快捷方式的jQuery代碼塊,並且不想全部更改,則可以將$符號作為參數傳遞給ready方法。 這允許您在此函數內部使用$訪問jQuery-在其外部,您將必須使用“ jQuery”:

 $.noConflict();
  jQuery(document).ready(function($)
  {
   var test = new Test({ 
    baseUrl: 'http://example.com', 
   interval: 15000,
   testId: 8,

    });
 }); 

暫無
暫無

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

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