完美解決jQuery符號$與其他javascript 庫、架構衝突的問題

來源:互聯網
上載者:User

標籤:conflict   javascrip   導致   comm   修改   var   com   解決   調用   

目前有大量的 javascript 開發架構,其中有一部分使用 $ 作為調用符號,這可能導致相互之間的衝突,而 jQuery 為解決這個問題,可以在 jQuery 匯入時放棄 $ 使用權,屆時 $ 則由其它架構使用,這樣可以避免相同名字的函數調用不再衝突。

jQuery 使用 noConflict 方法來放棄 $ 調用時的命名,之後由 jQuery 代替 $ 進行編寫。

例如:alert($(‘#message‘).val());

必須修改為 alert(jQuery(‘#message‘).val());

也可以重新定義 $ 進行調用

<script src="other_lib.js"></script><script src="jquery.js"></script><script>$.noConflict();jQuery( document ).ready(function( $ ) { // Code that uses jQuery‘s $ can follow here.});// Code that uses other library‘s $ can follow here.</script> 換成 j 作為調用符號var j = jQuery.noConflict();  // Do something with jQueryj( "div p" ).hide();  // Do something with another library‘s $()$( "content" ).style.display = "none";

完美解決jQuery符號$與其他javascript 庫、架構衝突的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.