Home > Web Front-end > JS Tutorial > body text

jQuery custom method of adding '$' and resolving '$' conflict_jquery

WBOY
Release: 2016-05-16 16:19:27
Original
1205 people have browsed it

The example in this article describes how to customize jQuery to add "$" and resolve "$" conflicts. Share it with everyone for your reference. The specific analysis is as follows:

1. Customize adding $

Although jQuery is very powerful, in any case, jQuery cannot meet the needs of all users, and some needs are very niche and are not suitable for inclusion in the entire jQuery framework. It is precisely because of this that jQuery provides users with Customize the method of adding "$".

The code is as follows:

Copy code The code is as follows:
$.fn.disable = function() {
return this.each(function() {
If (typeof this.disabled != "undefined") this.disable = true;
});
}

The above code first sets "$.fn.disable", indicating that "$" adds a method disable(), where "$.fn" is necessary to extend jQuery.

Then use an anonymous function to define this method, that is, use each() to set the disabled attribute of each element that calls this method to true. (If this attribute exists)

Example: Extending the functions of jquery

Copy code The code is as follows:




















Please select your gender:









What do you like to do:




















The method SwapInput(nName,oButton) judges based on the value of the button. If it is not available "disable", then dispatch disable() to set the element to be unavailable, and at the same time modify the value of the button to "enable", otherwise it will dispatch enable() method.

2. Resolve the conflict of "$"

Similar to the situation in the previous article, although JQuery is very powerful, sometimes developers use multiple frameworks at the same time. At this time, you need to be careful, because other frameworks may also use "$", resulting in conflicts. jQ also provides noConflict () method to solve the problem of "$" conflict.

Copy code The code is as follows:
jQuery.noconflict();

The above code can make "$" operate in the same way as other javascript frameworks. This means that "$" can no longer be used in jQuery, but "jQuery" must be used. For example, $("h2 a") must be written as jQuery("h2 a" ")

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

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template