Home Web Front-end JS Tutorial Two solutions to jQuery prototype conflicts (with demo sample download)_jquery

Two solutions to jQuery prototype conflicts (with demo sample download)_jquery

May 16, 2016 pm 03:19 PM
jquery conflict Solution

This article analyzes two solutions to jQuery prototype conflicts with examples. Share it with everyone for your reference, the details are as follows:

How can jquery and prototype conflict? In the final analysis, it is because both of them use $, and they are used at the same time, which is confusing. This problem has been solved no less than 5 times, and I have to check it every time. It doesn't hurt, hehe.

Method 1. Add code to the core library file of jquery.

1. It is usually jquery.js, or jquery.min.js, some with version numbers. Just know which file it is.

})( window );
jQuery.noConflict(); //最后面,加上这一行。

Copy after login

2. Load test jquery and prototype files

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"></script>
<script src="jquery.min.js"></script>

Copy after login

3. How to write js code

<script type="text/javascript">
alert('prototype value : '+$('test').value); //prototype写法
jQuery(document).ready(function($){ //注意这里的,jQuery和$
  alert('jquery value : '+$('#test').val()); //jquery写法
});
</script>

Copy after login

Recommended this method, this method is more effective once and for all

The complete demo code is as follows:

<html>
<head>
<script type="text/javascript" src="./prototype.js"></script>
<script src="jquery.min.js"></script>
</head>
<body>
<form>
<input id="test" type='text' name='test' value='test'/>
</form>
<script type="text/javascript">
alert('prototype value : '+$('test').value);
jQuery(document).ready(function($){
 alert('jquery value : '+$('#test').val());
});
</script>
</body>
</html>

Copy after login

Method 2: Resolve the conflict where jquery is called

1. Load test jquery and prototype files

//jquery和prototype,没有先后顺序,谁先谁后都一样。
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"></script>

Copy after login

2. js code

<script type="text/javascript">
jQuery.noConflict(); //解决冲突,这个一定要放在js代码的最前面,不然就会报错了。
alert('prototype value : '+$('test').value);
jQuery(document).ready(function($){
  alert('jquery value : '+$('#test').val());
});
</script>

Copy after login

This method is more suitable for situations where the core source files of jquery are not on your own server, or the jquery code is relatively small. The sample demoClick here to view.

The complete demo can be clicked hereDownload from this site.

I hope this article will be helpful to everyone in jQuery programming.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Printer operation cannot be completed error 0x0000709 Solution Printer operation cannot be completed error 0x0000709 Solution Apr 20, 2024 pm 10:10 PM

Printer operation cannot be completed error 0x0000709 Solution

Methods to solve the problem of Chinese garbled characters in PHP Dompdf Methods to solve the problem of Chinese garbled characters in PHP Dompdf Mar 05, 2024 pm 03:45 PM

Methods to solve the problem of Chinese garbled characters in PHP Dompdf

What should I do if my Black Shark phone continues to cycle on and off? The solution is revealed! What should I do if my Black Shark phone continues to cycle on and off? The solution is revealed! Mar 25, 2024 am 09:36 AM

What should I do if my Black Shark phone continues to cycle on and off? The solution is revealed!

Linux Oops Revealed: Causes of Errors and Solutions Linux Oops Revealed: Causes of Errors and Solutions Mar 20, 2024 am 11:15 AM

Linux Oops Revealed: Causes of Errors and Solutions

What does error code 678 mean and how to solve it What does error code 678 mean and how to solve it Feb 29, 2024 am 10:28 AM

What does error code 678 mean and how to solve it

Deepseek official website entrance access guide Solve the common problems that cannot be logged in Deepseek official website entrance access guide Solve the common problems that cannot be logged in Feb 19, 2025 pm 04:30 PM

Deepseek official website entrance access guide Solve the common problems that cannot be logged in

How to solve the problem of Pokemon Crystal, Diamond, Bright Pearl and Duck blocking the road? How to solve the problem of Pokemon Crystal, Diamond, Bright Pearl and Duck blocking the road? Apr 01, 2024 pm 02:33 PM

How to solve the problem of Pokemon Crystal, Diamond, Bright Pearl and Duck blocking the road?

Solution to PHP mb_substr function not executing Solution to PHP mb_substr function not executing Mar 22, 2024 am 11:54 AM

Solution to PHP mb_substr function not executing

See all articles