Loading jQuery
1. Insist on using CDN to load jQuery. Why not take advantage of other people’s servers to host your files for free? Click to view the benefits of using CDN, and click here to view some mainstream jQuery CDN addresses.
2. For security reasons, it is best to provide a local backup so that the website can work even when jQuery cannot be obtained from the remote CDN server, as shown in the above code. See details here.
3. Use the naked protocol URL (that is, remove http: or https:), as shown in the above code.
4. If possible, try to put your JavaScript and jQuery code at the bottom of the page. Click here for details, or view a standard HTML5 page template.
5. Which version should I use?
If you want to be compatible with IE678, please use version 2.x
For the lucky few who don’t need to consider compatibility, it is highly recommended to use the latest version of jQuery
When loading jQuery from a CDN server, it is best Write the full version (such as 1.11.0 instead of 1.11 or write 1 directly)
Never load it repeatedly
6. If you also use other JS frameworks such as Prototype, MooTools, Zepto Yunyun, because they The $ sign is also used, so you are no longer using the dollar sign for jQuery encoding, and use 'jQuery' instead. And calling $.noConflict() ensures that no conflicts will occur.
7. To check whether the browser supports some new features, please use Modernizr. Interstitial advertising: On the issue of not detecting browsers
About variables
1. It is best to add a $ prefix to jQuery type variables.
2. Always store the content returned by the jQuery selector into a variable for reuse
3. Use camel case naming
About selectors
1. Try to use ID selector. The mechanism behind it is actually to call the native document.getElementById(), so it is faster than other selectors.
2. Specify the type of element when using a class selector. If you don’t believe me, look at this performance comparison
3. To find child elements under the ID parent container, please use the .find() method. The reason this is fast is that selecting elements by id does not use the Sizzle engine. See here for details
4. In multi-level search, the right side should be as detailed as possible and the left side should be as simple as possible. Learn more
5. Avoid redundancy. Details or view performance comparison
6. Specify the context of selection.
7. The table uses a universal selector. View the detailed explanation
8. Be wary of implicit universal selectors. If omitted, the * wildcard character is actually used. More information
9.ID already represents uniqueness, and document.getElementById() is used behind it, so the table is mixed with other selectors.
DOM operation related
1. Uninstall any element from the document before operating it, and then paste it back again. This matter can be more detailed
// This is not good
var $myList = $("#list");
for(var i = 0; i < 10000; i ){
$myList.append("
3. Don’t deal with non-existent elements. Details
Event related
1. Only write one handler for the document ready event on a page. In this way, the code is clear and easy to debug, and it is easy to track the progress of the code.
2. The table uses anonymous functions as event callbacks. Anonymous functions are difficult to debug, maintain, test and reuse. Maybe you want to be more serious, take a look here
3. The callback for processing the document ready event also uses anonymous functions. Anonymous functions are difficult to debug, maintain, test and reuse: (
4. Further, it is best
to also put the ready event handler in an external file and introduce it to the page, and the code embedded in the page only needs to be called.
5. There are tens of millions of JS codes written inline to HTML, which is a debugging nightmare! You should always use jQuery to bind the event itself, so that you can dynamically unbind it at any time.
6. If possible, try to use a namespace when binding event handlers, so that you can easily unbind without affecting other bindings.
Asynchronous operation
1. Use $.ajax() directly instead of .getJson() or .get(), because jQuery still converts it to the former internally
2. Use HTTP to initiate requests for HTTPS sites. It is best to simply specify it (remove HTTP or HTTPS from your URL)
3. The table embeds parameters in the link, please use special parameter settings to pass
4. Try to specify the data type so that you know what kind of data you want to process (see the Ajax template mentioned below)
5. For asynchronously dynamically loaded content, it is best to use a proxy to bind event handlers. The advantage of this is that it is also valid for element events that are dynamically loaded later. You may want to know more
6. Use Promise mode. More examples
7. One point for standard Ajax template. Chasing the root cause
Animation and special effects
1. Maintain a consistent and unified animation implementation
2. Follow the user experience closely and avoid abusing animation special effects
Use simple show-hide, state switching, slide-in and slide-out effects to display elements
Use preset values to set the animation speed to 'fast', 'slow', or 400 (medium speed)
Plug-in related
1. Always choose a plugin that has good support, good documentation, thorough testing and an active community
2. Pay attention to whether the plug-in used is compatible with the jQuery version currently used
3. Some common functions should be written as jQuery plug-ins. One minute jQuery plug-in writing template
Chain syntax
1. In addition to using variables to save the results returned by the jQuery selector, you can also make good use of chain calls.
2. When there are more than 3 chain calls or the code is slightly complicated due to bound callbacks, use line breaks and appropriate indentation to improve the readability of the code.
3. For particularly long calls, it is best to use variables to save intermediate results to simplify the code.
Others
1. Use object literals to pass parameters
2. Mix CSS and jQuery
3. Always pay attention to the official Changelog and use the abandoned method.
4. Use native JavaScript when appropriate.