分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

jQuery.noConflict( )

发布时间:2023-09-06 02:26责任编辑:郭大石关键词:jQuery

jQuery.noConflict( )

Query.noConflict( [removeAll ] )

removeAll

Type: Boolean (default:false)

A Boolean indicating whether to remove all jQuery variables from the
global scope (including jQuery itself).

$.noConflict( )

  1. $.noConflict( ) default:false

Return control of $ back to the front library

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

Pass $ to the ready() callback function,within you can use $

<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>
jQuery.noConflict();(function( $ ) { ?$(function() { ???// More code using $ as alias to jQuery ?});})(jQuery); // Other code using $ as an alias to the other library

Create a different alias instead of jQuery to use in the rest of the script.

var jq = jQuery.noConflict(); // Do something with jQueryjq( "div p" ).hide(); // Do something with another library's $()$( "content" ).style.display = "none";

Completely move jQuery to a new namespace in another object.

var dom = {};dom.query = jQuery.noConflict( true );
  1. $.noConflict( true )

Return the globally scoped jQuery $ variables to the front version.

<script src="jQuery1.1.js"></script><script src="jquery3.0.js"></script><script>$.noConflict(true);// Code that uses another version`s "jQuery" "$" can follow here.</script>

Example

<script src="https://code.jquery.com/jquery-1.10.2.js"></script> <div id="log"> ?<h3>Before $.noConflict(true)</h3></div><script src="https://code.jquery.com/jquery-1.6.2.js"></script> <script>var $log = $( "#log" ); $log.append( "2nd loaded jQuery version ($): " + $.fn.jquery + "<br>" ); // Restore globally scoped jQuery variables to the first version loaded// (the newer version) jq162 = jQuery.noConflict( true ); $log.append( "<h3>After $.noConflict(true)</h3>" );$log.append( "1st loaded jQuery version ($): " + $.fn.jquery + "<br>" );$log.append( "2nd loaded jQuery version (jq162): " + jq162.fn.jquery + "<br>" );</script> 

Outcome

Before $.noConflict(true)2nd loaded jQuery version ($): 1.6.2After $.noConflict(true)1st loaded jQuery version ($): 1.10.22nd loaded jQuery version (jq162): 1.6.2

References
http://api.jquery.com/jQuery.noConflict/

SourceCode
jQuery.noConflict()

jQuery.noConflict( )

原文地址:https://www.cnblogs.com/rosendolu/p/10140883.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved