Built-in functions in modern browsers can replace jQuery.
The size of jQuery is constantly increasing. New features must be constantly added, which is an inevitable result. Although the slimming effect has been obvious since version 1.8.3, it is undeniable that it is still unacceptable for the development of web pages on mobile phones. Of course, jQuery is not a piece of iron. You can customize it and package it into the components you want, but some of the code used to be compatible with older browsers cannot be removed.
As far as my personal habits are concerned, no matter what projects I develop, even a very simple demo, the first thing I do is to introduce jQuery, in this way, we want to use the DOM selector function provided by it. This method is obvious for some older browsers such as IE6/IE7, but now modern browsers have built in the complete DOM selector function, this allows you to use the methods provided by the native browser to implement jQuery functions. Share the best UI front-end framework!
Document. queryselector
The modern browser here refers to the new generation of browsers such as Firefox, Google, Opera, and Safri. If it is IE, you must at least IE8 or later. You can map the document. querySelector () function$It returns the first element found in the DOM that matches the selection condition. Any CSS selector can be used as its parameter.
Note: IE 8 only supports the CSS2.1 standard selector.
. Code
-
-
-
- Pink
-
- Salmon
-
- Blue
-
- Green
-
- Red
-
-
- <Script>
- // Create a global '$' variable
- Window. $ = function (selector ){
- Return document. querySelector (selector );
- };
- (Function (){
- // Search for item1 by id and change its background color to light red
- Var item = $ ("# salmon"). style. backgroundColor = "salmon ";
- Console. log (item );
- }());
- </Script>
You need to use the native "style" method, and the chain call in jQuery cannot be used here. Console. log () Outputs "salmon" returned by the backgroundColor method ".
The native DOM node is sometimes better than the encapsulated jQuery object. For example, if you want to modify the src attribute of an image, you can compare the jQuery method and the DOM Node Method. Share the best UI front-end framework!
. Code
- // JQuery
- $ ("# Picture"). attr ("src", "http://placekitten.com/200/200 ");
- // Use the native js method that maps querySelector to $
- $ ("# Picture"). src = "http://placekitten.com/200/200 ";
DOM objects allow you to directly access the src attribute of images. On the contrary, all jQuery objects need to be operated through the attr function. The document. querySelector method returns only one element. If your selection target is a bunch of elements, it will only return the first node that meets the conditions. To return all nodes, you must use the document. querySelectorAll method.
Document. queryselectorallA clever practice is to map querySelector to $ and querySelectorAll to $. However, it returns a node list, which is not as easy as the Array format returned by jQuery. We can use the Array. prototype. slice. call (nodeList) method for processing to make it more convenient.
. Code
-
-
-
- Pink
-
- Salmon
-
- Blue
-
- Green
-
- Red
-
-
- <Script>
- Window. $ = function (selector ){
- Return document. querySelector (selector );
- };
- Window. $ = function (selector ){
- Var items = {},
- Results = [],
- Length = 0,
- I = 0;
- // Note that IE8 does not support this method
- Results = Array. prototype. slice. call (document. querySelectorAll (selector ));
- Length = results. length;
- // Add the results to the items object
- For (; I <length ;){
- Items [I] = results [I];
- I ++;
- }
- // Add some additional attributes to make it more like an Array
- Items. length = length;
- Items. splice = []. splice ();
- // Add the 'REACH' Method
- Items. each = function (callback ){
- Var I = 0;
- For (; I <length ;){
- Callback. call (items [I]);
- I ++;
- }
- }
- Return items;
- };
- (Function (){
- // Search for the green option and modify the font size
- $ ("# Green"). style. fontSize = "2em ";
- // Use id Search to modify the background color
- $ ("Li"). each (function (){
- This. style. backgroundColor = this. id;
- });
- }());
- </Script>
Note that IE8 does not support converting a nodeList to an Array.
ClassListJQuery is very convenient for CSS class operations. Fortunately, modern browsers also have built-in methods to operate them conveniently, mainly using classList objects. The following is a comparison of the two basic operations. Share the best UI front-end framework!
. Code
- Window. $ = function (selector ){
- Return document. querySelector (selector );
- };
- // ---- Add the CSS class ------
- /* JQuery */
- $ (". Main-content"). addClass ("someClass ");
- /* Equivalent built-in method */
- $ (". Main-content"). classList. add ("someClass ");
- // ---- Delete a CSS class -----
- /* JQuery */
- $ (". Main-content"). removeClass ("someClass ");
- /* Equivalent built-in method */
- $ (". Main-content"). classList. remove ("someClass ");
- // ---- Determine whether a CSS class exists ---
- /* JQuery */
- If ($ (". main-content"). hasClass ("someClass "))
- /* Equivalent built-in method */
- If ($ (". main-content"). classList. contains ("someClass "))
Compared with jQuery's rich methods, the built-in functions in the modern browsers mentioned above are very simple. However, if the requirements in the project are not high, these alternative methods can greatly reduce your application dependencies.
Note that these built-in methods are supported differently in different browsers and versions. The following table lists their support levels. Share the best UI front-end framework!
Figure 1: Compatibility of various browsers with querySelector/querySelectorAllPreview = "title =" click to view original size picture "class =" alignnone size-full wp-image-6534 overflow-img-right magplus "src =" http://www.bkjia.com/uploads/allimg/140921/042P5M33-0.jpg ">
Figure 1: Compatibility of various browsers with classlist