Perfect solution to jQuery symbol $ conflicts with other javascript libraries and frameworks, jqueryjavascript

Source: Internet
Author: User

Perfect solution to jQuery symbol $ conflicts with other javascript libraries and frameworks, jqueryjavascript

Currently, there are a large number of javascript development frameworks, some of which use $ as the call symbol, which may lead to conflicts between each other. jQuery solves this problem, you can discard the $ permission when importing jQuery. Then, $ is used by other frameworks. This prevents function calls with the same name from conflict.

JQuery uses the noConflict method to discard the name when calling $, and then uses jQuery to replace $ for writing.

For example:Alert ($ ('# message'). val ());

It must be changed to alert (jQuery ('# message'). val ());

JQuery can run normally.

<Script src = "other_lib.js"> </script> <script src = "jquery. js "> </script> <script> $. noConflict (); // you can only use jQuery to call jQuery ('# message '). val (); </script>

You can also redefine $ for calling.

<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>

Change to j as the call symbol

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

The above is a perfect solution to the conflict between jQuery symbol $ and other javascript libraries and frameworks. It is all the content shared by the editor. I hope you can give us a reference and support for more.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.